[PyQt] Trying to learn ...

Magnus Wirström asdlinux71 at gmail.com
Tue Aug 2 23:54:35 BST 2011


2011/8/2 Hans-Peter Jansen <hpj at urpla.net>:
> Magnus, please keep the conversation on the ML or CC at the very least.
> Others might want to participate.
>
> My personal preference is to NOT receive personal copies.

Sorry ... first time using mailing list so i just used reply button,
my bad. Forgive me

>
> On Tuesday 02 August 2011, 11:20:15 Magnus Wirström wrote:
>> 2011/8/2 Hans-Peter Jansen <hpj at urpla.net>:
>> > 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.
>>
>> It prints nothing in the console when i start up...
>
> Ah, of course not.
>
> 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.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())
>>
>> This was an attempt to get it to work. I added this and forgot to
>> remove it. What i understand of the tutorial, the bindings to the
>> slots would be done automatic by this line in  the script above
>> "QtCore.QMetaObject.connectSlotsByName(Dialog)".
>
> Yes. For that reason, if you try my fixed script, you get the Hello
> twice, once from the manual connection, and one from the automatic
> connection.
>
>> I think my question
>> is why this is not working.
>
> Your app is missing a major part: the usual if __name__ is "__main__":
> Check out the ui file. Not only it carries some such, it is also using
> sane import clauses.


This seems to do the trick ... i have now an working app that responds
to signals
Thanks for the help :)

I will now try to learn pyqt :)
Magnus

>
>> >
>> >  self.pushButton.clicked.connect(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?
>>
>> Yes ... it looks much nicer... I tried to replace my line with yours
>> but still no joy... still the same result... nothing happens. if i
>> press the pushbutton a couple of times i get this message in the
>> console:
>>
>> QDialog::exec: Recursive call detected
>>
>> When i start to get it ... it appears on every click.
>
> This is confusing. You need to run the testui.py script, not the
> Ui_testui.py script for this to work. The latter is just the dialog
> definition (with some independent test code supplied), the former is
> the right place for your code.
>
>> > ("new, as in "only a couple of years old…")
>> >
>> > And it should do, what you want, does it?
>>
>> No ... still no joy :(
>>
>
> Hth,
> Pete
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>


More information about the PyQt mailing list