3

Following this tutorial as a guide, I created a plugin using the Plugin Builder with the dock widget form. I am now trying to connect a pushButton to a function. The following is a snippet of code:

def __init__(self, iface):
 self.dockwidget.pushButton.clicked.connect(self.test_print)
def test_print(self):
 print 'It works!'

However, I receive an:

error when calling its classFactory() method

I have also tried:

def initGui(self):
 QObject.connect(self.dockwidget.pushButton, SIGNAL("clicked()"), self.test_print)

But then there's an error calling its initGui() method. Any pointers?


Edit:

I have tried the following variations in the run(self) function, I no longer receive errors but nothing is printed in the console:

def run(self):
 #self.dockwidget.connect(self.dockwidget.pushButton,SIGNAL("clicked()"),self.test_print)
 #self.dockwidget.pushButton.clicked.connect(self.test_print)
 QObject.connect(self.dockwidget.ui.pushButton, SIGNAL("clicked()"), self.test_print)
asked Apr 26, 2016 at 11:59
6
  • Did you try to put the connect in the run(self) method ? Commented Apr 26, 2016 at 12:11
  • @Azimo - Thanks, just tested it. I no longer receive the errors but nothing is printed in the console. Commented Apr 26, 2016 at 12:16
  • QgsMessageLog.logMessage('It works!','test') ?? Commented Apr 26, 2016 at 12:23
  • @Azimo - Not showing up when clicking the pushButton unfortunately. Works fine if I stick it in the run(self) function, which just loads the message log so atleast something happens! Commented Apr 26, 2016 at 12:38
  • But, can you confirm that the "objectName" property of the QPushButton (in your .ui file) is "pushButton". Commented Apr 26, 2016 at 12:43

2 Answers 2

3
import os
from PyQt4 import QtGui, uic
from PyQt4.QtCore import pyqtSignal
FORM_CLASS, _ = uic.loadUiType(os.path.join(
 os.path.dirname(__file__), 'my_dockwidget_base.ui'))
class myDockWidget(QtGui.QDockWidget, FORM_CLASS):
 closingPlugin = pyqtSignal()
 def __init__(self, parent=None):
 """Constructor."""
 super(myDockWidget, self).__init__(parent)
 # Set up the user interface from Designer.
 # After setupUI you can access any designer object by doing
 # self.<objectname>, and you can use autoconnect slots - see
 # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
 # #widgets-and-dialogs-with-auto-connect
 self.setupUi(self)
 def closeEvent(self, event):
 self.closingPlugin.emit()
 event.accept()

from PyQt4.QtCore import * #QSettings, QTranslator, qVersion, QCoreApplication, Qt
from PyQt4.QtGui import * #QAction, QIcon
from qgis.core import *
# Initialize Qt resources from file resources.py
import resources
# Import the code for the DockWidget
from my_dockwidget import myDockWidget
import os.path
class myPlugin:
 # cutting other methods (__init__, initGui, unload..etc..)
 def run(self):
 if not self.pluginIsActive:
 self.pluginIsActive = True
 # openOtherWin is outside of run method...
 self.dockwidget.but_openOtherWin.clicked.connect(self.openOtherWin)

answered Apr 27, 2016 at 11:17
3
  • Thanks for posting, I will try testing this at some point! Commented Apr 27, 2016 at 11:18
  • Ok... Adding now the .py of plugin class ... Commented Apr 27, 2016 at 11:22
  • I created a test plugin and used your method and it worked. Interesting, I will accept your answer but will also keep mine in case it could work for others ;) Commented Apr 29, 2016 at 10:54
3

Guess the answer was simpler than I thought. Had to insert everything into the run(self) function. So the following code works:

def run(self):
 def test_print():
 print 'It works!'
 self.dockwidget.pushButton.clicked.connect(test_print)
answered Apr 26, 2016 at 14:32
4
  • Not for me, strange.... :/ Commented Apr 27, 2016 at 10:55
  • @Azimo - Haha indeed, were you also using dockwidgets or a dialog form? Commented Apr 27, 2016 at 10:59
  • dock... I copy .py in an answer and will delete after... Commented Apr 27, 2016 at 11:16
  • @Azimo - Keep the answer in please. If it works for you then it could work for others in a similar situation ;) Commented Apr 27, 2016 at 11:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.