Skip to content

Instantly share code, notes, and snippets.

@Guts
Last active December 24, 2015 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Guts/10a2944f8222cc35baa8 to your computer and use it in GitHub Desktop.
Save Guts/10a2944f8222cc35baa8 to your computer and use it in GitHub Desktop.
Code du tutoriel de GeoTribu pour créer un plugin QGIS
mapCan = qgis.utils.iface.mapCanvas()
mapCan.layerCount()
>>> 1
lay = mapCan.layer(0)
print lay.name()
>>> TM_WORLD_BORDERS-0.3
lay.setLayerName("World Border")
print lay.name()
>>> World Border
# run method that performs all the real work
def run(self):
mapCanvas = self.iface.mapCanvas()
if mapCanvas.layerCount() == 0:
QMessageBox.warning(self.iface.mainWindow(), "Show Active Layer Plugin" , ("No active layer found"), QMessageBox.Ok)
return layerName = ""
for i in range(mapCanvas.layerCount()-1,-1,-1):
layer = mapCanvas.layer(i)
layerName += layer.name()+"\n"
# create and show the dialog
dlg = showActiveLayerDialog()
dlg.ui.plainTextEdit.appendPlainText(layerName)
dlg.show()
result = dlg.exec_()
# See if OK was pressed
if result == 1:
QMessageBox.warning(self.iface.mainWindow(), "Show Active Layer Plugin", ("Voulez allez quitter le plugin !"), QMessageBox.Ok)
# compile QT to Python classes (PyQt)
pyuic4 -o UI_isogeo_dockwidget_base.py -x isogeo_dockwidget_base.ui
pyrcc4 -o resources_rc.py resources.qrc
# changed with last plugin builder
# (source: http://anitagraser.com/2014/04/26/getting-started-writing-qgis-2-x-plugins/)
pyrcc4 -o resources.py resources.qrc
from PyQt4 import QtCore, QtGui
class Ui_showActiveLayer(object):
def setupUi(self, showActiveLayer):
showActiveLayer.setObjectName("showActiveLayer")
showActiveLayer.resize(389, 279)
self.buttonBox = QtGui.QDialogButtonBox(showActiveLayer)
self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.plainTextEdit = QtGui.QPlainTextEdit(showActiveLayer)
self.plainTextEdit.setGeometry(QtCore.QRect(10, 30, 361, 201))
self.plainTextEdit.setObjectName("plainTextEdit")
self.label = QtGui.QLabel(showActiveLayer)
self.label.setGeometry(QtCore.QRect(10, 10, 181, 17))
self.label.setObjectName("label")
self.retranslateUi(showActiveLayer)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), showActiveLayer.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), showActiveLayer.reject)
QtCore.QMetaObject.connectSlotsByName(showActiveLayer)
def retranslateUi(self, showActiveLayer):
showActiveLayer.setWindowTitle(QtGui.QApplication.translate("showActiveLayer", "showActiveLayer", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("showActiveLayer", "Les couches actives sont :", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
showActiveLayer = QtGui.QDialog()
ui = Ui_showActiveLayer()
ui.setupUi(showActiveLayer)
showActiveLayer.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment