Plugin Qgis - bottone "hello world"
« il: Settembre 15, 2012, 19:55 »
Ciao a tutti,

ho creato un plugin per QGIS in python tramite il PluginBuilder e con QtDesigner ho applicato un bottone (QPushButton), adesso vorrei che al click comparisse una finestra di dialogo con la scritta Hello World, un classico insomma...

Codice primoPlugin.py:

[codice]# Import the PyQt and QGIS libraries
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
# Initialize Qt resources from file resources.py
import resources
# Import the code for the dialog
from primoplugindialog import primoPluginDialog

class primoPlugin:

    def __init__(self, iface):
        # Save reference to the QGIS interface
        self.iface = iface

    def initGui(self):
        # Create action that will start plugin configuration
        self.action = QAction(QIcon(":/plugins/primoplugin/icon.png"), \
            "cdq", self.iface.mainWindow())
        # connect the action to the run method
        QObject.connect(self.action, SIGNAL("triggered()"), self.run)

        # Add toolbar button and menu item
        self.iface.addToolBarIcon(self.action)
        self.iface.addPluginToMenu("&cdq", self.action)

    def unload(self):
        # Remove the plugin menu item and icon
        self.iface.removePluginMenu("&cdq",self.action)
        self.iface.removeToolBarIcon(self.action)

    def clickEvent(self):
        QMessageBox.information(self.iface.mainWindow(), "Info", "HELLO WORLD")


    # run method that performs all the real work
    def run(self):

        # create and show the dialog
        dlg = primoPluginDialog()
        # show the dialog
        dlg.show()
        result = dlg.exec_()
        # See if OK was pressed
        if result == 1:
            # do something useful (delete the line containing pass and
            # substitute with your code
            pass[/codice]

Codice ui_primoplugin:

[codice]from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_primoPlugin(object):
    def setupUi(self, primoPlugin):
        primoPlugin.setObjectName(_fromUtf8("primoPlugin"))
        primoPlugin.resize(400, 290)
        self.buttonBox = QtGui.QDialogButtonBox(primoPlugin)
        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(_fromUtf8("buttonBox"))
        self.pushButton = QtGui.QPushButton(primoPlugin)
        self.pushButton.setGeometry(QtCore.QRect(110, 100, 191, 101))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.label = QtGui.QLabel(primoPlugin)
        self.label.setGeometry(QtCore.QRect(150, 50, 101, 31))
        self.label.setObjectName(_fromUtf8("label"))

        self.retranslateUi(primoPlugin)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), primoPlugin.accept)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), primoPlugin.reject)
        QtCore.QMetaObject.connectSlotsByName(primoPlugin)

    def retranslateUi(self, primoPlugin):
        primoPlugin.setWindowTitle(QtGui.QApplication.translate("primoPlugin", "primoPlugin", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("primoPlugin", "Click", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("primoPlugin", "click for \"hello world\"", None, QtGui.QApplication.UnicodeUTF8))[/codice]

Come faccio a collegare il bottone alla funzione che genera il messaggio?

Maurizio