[PySide] connecting to the QObject.destroyed signal

579 views
Skip to first unread message

Erik Janssens

unread,
Jul 23, 2011, 5:16:32 PM7/23/11
to pyside
Hi,

when connecting a Python method to the
QObject.destroyed signal, it seems as
if the connected slot is only called with
one argument instead of two, this results
in :

TypeError: destroyed_slot() takes exactly 2 arguments (1 given)
Error calling slot "destroyed_slot"

while I would expect 2 arguments (self and
the object being destroyed).

am I missing something or is this possibly
a bug ?

Thanks,

Erik


_______________________________________________
PySide mailing list
PyS...@lists.pyside.org
http://lists.pyside.org/listinfo/pyside

Sebastian Wiesner

unread,
Jul 24, 2011, 5:00:44 AM7/24/11
to erik.j...@conceptive.be, pyside
2011/7/23 Erik Janssens <Erik.J...@conceptive.be>:

> Hi,
>
> when connecting a Python method to the
> QObject.destroyed signal, it seems as
> if the connected slot is only called with
> one argument instead of two, this results
> in :
>
> TypeError: destroyed_slot() takes exactly 2 arguments (1 given)
> Error calling slot "destroyed_slot"
>
> while I would expect 2 arguments (self and
> the object being destroyed).
>
> am I missing something or is this possibly
> a bug ?

The "destroyed()" signal is overloaded, so there are actually two
signals "destroyed()" and "destroyed(QObject)". You apparently
connected to the former. In order to connect to the latter, you need
to explicitly choose the right signature:
"obj.destroyed[QObject].connect(self.destroyed_slot)"

Erik Janssens

unread,
Jul 24, 2011, 7:04:06 AM7/24/11
to Sebastian Wiesner, pyside
On Sun, 2011-07-24 at 11:00 +0200, Sebastian Wiesner wrote:
> 2011/7/23 Erik Janssens <Erik.J...@conceptive.be>:
> > Hi,
> >
> > when connecting a Python method to the
> > QObject.destroyed signal, it seems as
> > if the connected slot is only called with
> > one argument instead of two, this results
> > in :
> >
> > TypeError: destroyed_slot() takes exactly 2 arguments (1 given)
> > Error calling slot "destroyed_slot"
> >
> > while I would expect 2 arguments (self and
> > the object being destroyed).
> >
> > am I missing something or is this possibly
> > a bug ?
>
> The "destroyed()" signal is overloaded, so there are actually two
> signals "destroyed()" and "destroyed(QObject)". You apparently
> connected to the former. In order to connect to the latter, you need
> to explicitly choose the right signature:
> "obj.destroyed[QObject].connect(self.destroyed_slot)"

You are right, choosing the right signature works. But how do
you know this signal is overloaded, I cannot see this mentioned
in the docs ?

Sebastian Wiesner

unread,
Jul 24, 2011, 8:17:45 AM7/24/11
to erik.j...@conceptive.be, pyside
2011/7/24 Erik Janssens <Erik.J...@conceptive.be>:

> On Sun, 2011-07-24 at 11:00 +0200, Sebastian Wiesner wrote:
>> 2011/7/23 Erik Janssens <Erik.J...@conceptive.be>:
>> > Hi,
>> >
>> > when connecting a Python method to the
>> > QObject.destroyed signal, it seems as
>> > if the connected slot is only called with
>> > one argument instead of two, this results
>> > in :
>> >
>> > TypeError: destroyed_slot() takes exactly 2 arguments (1 given)
>> > Error calling slot "destroyed_slot"
>> >
>> > while I would expect 2 arguments (self and
>> > the object being destroyed).
>> >
>> > am I missing something or is this possibly
>> > a bug ?
>>
>> The "destroyed()" signal is overloaded, so there are actually two
>> signals "destroyed()" and "destroyed(QObject)".  You apparently
>> connected to the former.  In order to connect to the latter, you need
>> to explicitly choose the right signature:
>> "obj.destroyed[QObject].connect(self.destroyed_slot)"
>
> You are right, choosing the right signature works.  But how do
> you know this signal is overloaded, I cannot see this mentioned
> in the docs ?

I don't know about the PySide documentation, but you can easily see
that in the Qt documentation [1]. The signature of "destroyed" is
"QObject::destroyed(QObject *obj=0)". The argument "obj" has a
default value, and C++ implements default values for arguments by
generating overloaded functions.

[1] http://doc.qt.nokia.com/latest/qobject.html#destroyed

Erik Janssens

unread,
Jul 24, 2011, 9:49:50 AM7/24/11
to Sebastian Wiesner, pyside

Thank you for pointing this out, I was unaware of this
'implementation detail', it seems PyQt handles this case
different.

Sebastian Wiesner

unread,
Jul 24, 2011, 9:57:30 AM7/24/11
to erik.j...@conceptive.be, pyside

Really? PyQt should actually handle this case in exactly the same way.

Erik Janssens

unread,
Jul 24, 2011, 3:04:01 PM7/24/11
to Sebastian Wiesner, pyside

this code always worked perfectly with PyQt. I had the assumption that
a default argument in a signal would be treated the same way as in
Python, this assumption always seemed valid using PyQt

Sebastian Wiesner

unread,
Jul 24, 2011, 6:16:53 PM7/24/11
to erik.j...@conceptive.be, pyside
2011/7/24 Erik Janssens <Erik.J...@conceptive.be>:

> this code always worked perfectly with PyQt.  I had the assumption that
> a default argument in a signal would be treated the same way as in
> Python, this assumption always seemed valid using PyQt

I guess, PyQt just chooses the other overload as default if no
overload was explicity selected. Since you can always use slots with
less arguments than the signal (in which case superfluous arguments
are simply thrown away during invocation of the slot) it makes no
difference in this case.

Erik Janssens

unread,
Jul 26, 2011, 11:00:12 AM7/26/11
to Sebastian Wiesner, pyside
on second thought, I think that PyQt looks at the signature of the
slot and tries to match the signal and slot with the same signature.

it raises an exception at connection time when this match fails.

so this seems to be a very different behavior in PySide where the
connect succeeds but the failure is later on when the signal is
emitted ?

Reply all
Reply to author
Forward
0 new messages