[PyQt] Trying to learn ...

Hans-Peter Jansen hpj at urpla.net
Tue Aug 2 01:02:24 BST 2011


On Monday 01 August 2011, 22:20:32 Magnus Wirström wrote:
> Hi
>
> I am sorry if this have been asked before :)
>
> I am trying to learn using python and pyqt using ERIC 5.1.2. I have a
> problem with signals and slots. I tried this on 2 different computer
> with different OS and different version of python. My problem is
> this...
>
> I am creating a "test" project using Eric ... using one of the
> tuturials on Eric's webpage. I create a project and a form, then i
> add a push button. I save the form and i generate the form into code
> using Eric I also generate dialog code and adding a slot for my push
> button. So far i have not written a single line of code. In my slot i
> write a print("Hello"), My only self written code. When i run this, i
> get a window with my button but when i click it nothing happens. When
> have i done wrong? How can i get my button to execute the slot?

You should get the "Hello" printed in console on start up, don't you?

See below.

> thanks for the help ... sorry if this is a stupid question ;)
>
> Here is the code:
>
>
> # Form implementation generated from reading ui file
> '/home/magnus/eric/test/testui.ui'
> #
> # Created: Mon Aug  1 22:16:17 2011
> #      by: PyQt4 UI code generator 4.8.4
> #
> # WARNING! All changes made in this file will be lost!
>
> from PyQt4 import QtCore, QtGui
>
> try:
>     _fromUtf8 = QtCore.QString.fromUtf8
> except AttributeError:
>     _fromUtf8 = lambda s: s
>
> class Ui_Dialog(object):
>     def setupUi(self, Dialog):
>         Dialog.setObjectName(_fromUtf8("Dialog"))
>         Dialog.resize(400, 300)
>         self.pushButton = QtGui.QPushButton(Dialog)
>         self.pushButton.setGeometry(QtCore.QRect(140, 130, 83, 25))
>         self.pushButton.setObjectName(_fromUtf8("pushButton"))
>
>         self.retranslateUi(Dialog)
>         QtCore.QObject.connect(self.pushButton,
> QtCore.SIGNAL(_fromUtf8("clicked()")), Dialog.exec)
>         QtCore.QMetaObject.connectSlotsByName(Dialog)
>
>     def retranslateUi(self, Dialog):
>         Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog",
> "Dialog", None, QtGui.QApplication.UnicodeUTF8))
>        
> self.pushButton.setText(QtGui.QApplication.translate("Dialog",
> "PushButton", None, QtGui.QApplication.UnicodeUTF8))
>
>
> if __name__ == "__main__":
>     import sys
>     app = QtGui.QApplication(sys.argv)
>     Dialog = QtGui.QDialog()
>     ui = Ui_Dialog()
>     ui.setupUi(Dialog)
>     Dialog.show()
>     sys.exit(app.exec_())
>
>
> ---------------------------------------------------------------------
>----------------------------------------------------------------------
>----------------------------------------------------------------------
>
>
> # -*- coding: utf-8 -*-
>
> """
> Module implementing Dialog.
> """
>
> from PyQt4.QtCore import *
> from PyQt4.QtGui import QDialog
>
> from .Ui_testui import Ui_Dialog
>
> class Dialog(QDialog, Ui_Dialog):
>     """
>     Class documentation goes here.
>     """
>     def __init__(self, parent = None):
>         """
>         Constructor
>
>         @param parent reference to the parent widget (QWidget)
>         """
>         QDialog.__init__(self, parent)
>         self.setupUi(self)
>         QtCore.QObject.connect(self.pushButton,
> QtCore.SIGNAL(_fromUtf8("clicked()")), self.on_pushButton_clicked())

                                                                   ^^
You're calling the signal handler here, resulting in connecting the 
signal to None. While at it, use the "new" style signals/slots:

	self.pushButton.clicked.connect(self.on_pushButton_clicked)

Looking much nicer, doesn't it? 

("new, as in "only a couple of years old…")

And it should do, what you want, does it?

>     @pyqtSlot()
>     def on_pushButton_clicked(self):
>         """
>         Slot documentation goes here.
>         """
>         print("hello")

Pete


More information about the PyQt mailing list