[PySide] PyQt. How to shoot yourself in the foot

Andrei Kopats hlamer at tut.by
Mon Aug 25 10:35:38 CEST 2014


Hi, Anatoly

See my comments below


> C++ object is deleted by Qt but Python wrapper still exists
> ...
> > A slot of the deleted object is called by timer or signal from the
> external world
>
> So, the correct strategy is to disconnect all signals and reset timers
> when an
> object is destroyed. I fail to see how C++ -- Python link play major
> role in this architecture. Even if you implement decoupled system with
> signals
> and timers in pure Python, it will still be flaky when referenced objects
> are
> destroyed. So how Qt propose to handle this on pure C++ level?
>
QObject::~QObject documentation: "All signals to and from the object are
automatically disconnected, and any pending posted events for the object
are removed from the event queue. "


> > C++ method overridden by Python wrapper
>
> Crashes. Looks like a missing part in PySide. If it is fixable, it needs a
> bug
> report to keep track of.
>
I don't this it is easily fixable, would has been fixed otherwise.

>
> Few more questions (I don't have CS degree, so they may be lame):
> > When deleting an object which has Python-slots, disconnect the slots
> manually
>
> How to distinguish object with Python-slots from, well, object with
> standard
> PySide slots?
>
When I say "Python slots" in this article, I mean any function or method,
which is written in Python and connected to signal. Not Python object model
__slots__.

>
> > To be notified about an object deletion use the QObject.destroyed signal
> but not
> > the __del__ method of a Python wrapper
>
> How to be notified with a __del__ method?
>
One may try to disconnect signals in __del__ method. i.e.
class Foo:
    def __del__(self)
        someObject.someSignal.disconnect(self.mySlot)
But this approach doesn't work.


> > Don't use QTimer.singleShot for an object which might be deleted. It is
> impossible
> > to stop and disconnect such a timer
>
> Not clear why singleShot, being connected to an object method, doesn't
> keep a
> reference to this object.
>
> > Don't use lambda function as a slot. It is impossible to disconnect it.
>
> Also interesting to know the gory details - why? If signals can reach
> it, then in theory a disconnector could reach it too.
>
All this would work fine, if Qt were written in pure Python. But the
C++-Python bridge introduces some magic and some pitfalls.

Regards,
Andrei
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20140825/8b563d41/attachment.html>


More information about the PySide mailing list