[PyQt] Question about the new Signals / Slots mechanism.
Phil Thompson
phil at riverbankcomputing.com
Mon Jan 31 18:52:59 GMT 2011
On Mon, 31 Jan 2011 19:33:04 +0100, Thomas Mansencal
<thomas.mansencal at gmail.com> wrote:
> Hello,
>
> I wanted to update my code to use the new Signals / Slots mechanism but
I'm
> running into some troubles that I don't manage to understand.
>
> Here is the snippet that I use :
>
> import inspect
>
> import functools
>
> import sys
>
> from PyQt4 import QtGui, QtCore
>
>
>> def getArgs(object_):
>
> return inspect.getargspec(object_).args
>
>
>> def executionTrace(object_):
>
> def function(*args, **kwargs):
>
> print "*" * 96
>
> print object_
>
> print args, kwargs
>
> print getArgs(object_)
>
> value = object_(*args, **kwargs)
>
> return value
>
> return function
>
>
>> class Buttons(QtGui.QWidget):
>
> def __init__(self, parent=None):
>
> QtGui.QWidget.__init__(self, parent)
>
>
>> self.setWindowTitle("Buttons")
>
>
>> a_Button = QtGui.QPushButton("A", self)
>
> a_Button.setGeometry(0, 0, 60, 30)
>
> a_Button.clicked.connect(self.doStuff)
>
>
>> b_Button = QtGui.QPushButton("B", self)
>
> b_Button.setGeometry(0, 30, 60, 30)
>
> b_Button.clicked.connect(lambda: self.doStuff())
>
>
>> c_Button = QtGui.QPushButton("C", self)
>
> c_Button.setGeometry(0, 60, 60, 30)
>
> self.connect(c_Button, QtCore.SIGNAL('clicked()'), self.doStuff)
>
>
>> @executionTrace
>
> def doStuff(self):
>
> stuff("1", "2", "3", "4")
>
>
>> def stuff(a, b, c, d):
>
> pass
>
>
>> app = QtGui.QApplication(sys.argv)
>
> qb = Buttons()
>
> qb.show()
>
> sys.exit(app.exec_())
>
>
> executionTrace is decorating the doStuff method, now I'm launching the
app
> and I click the buttons in the A, B, C order and here is what I get :
>
> Button A : Failing but that's the way I would have done it ( Notice the
> False argument that is coming from I don't know where :| )
>
>>
>>
************************************************************************************************
>
> <function doStuff at 0x023731F0>
>
> (<__main__.Buttons object at 0x01FDCB28>, False) {}
>
> Traceback (most recent call last):
>
> File "D:\Datas\Personal\Developement\Others\src\Tests.py", line 15, in
>> function
>
> value = object_(*args, **kwargs)
>
> TypeError: doStuff() takes exactly 1 argument (2 given)
>
> ['self']
>
>
>
> Button B : Working but I still don't know why.
>
>>
>>
************************************************************************************************
>
> <function doStuff at 0x023731F0>
>
> (<__main__.Buttons object at 0x01FDCB28>,) {}
>
> ['self']
>
>
>
> Button C : Working, and the way I was doing it before.
>
>>
>>
************************************************************************************************
>
> <function doStuff at 0x023731F0>
>
> (<__main__.Buttons object at 0x01FDCB28>,) {}
>
> ['self']
>
>
> If someone wanna shed some light on that, I'll be very happy :)
>
> Thanks,
>
> Thomas
The clicked() signal passes an argument. When a slot is a Python method
PyQt will call it with the argument and, if that fails, call it again
without the argument. My guess would be that your decorator is getting in
the way of that mechanism.
Phil
More information about the PyQt
mailing list