[PyQt] Bug with non-QObject signal callbacks

Robert Kern robert.kern at gmail.com
Thu Jul 16 22:08:51 BST 2009


I've tried to connect an instance method as a new-style signal callback. This 
works if I do nothing else, but if I happen to call a numpy method after 
connecting the callback, I get an error from PyQt, but during the numpy call:

Traceback (most recent call last):
   File "signal_bug.py", line 30, in <module>
     main()
   File "signal_bug.py", line 24, in main
     nqo = NotQObject(slider)
   File "signal_bug.py", line 14, in __init__
     a = numpy.arange(10).max()
TypeError: NotQObject cannot be converted to PyQt4.QtCore.QObject in this context


There are some places in numpy where we check to see if anyone has set an 
exception. I suspect that what is happening is that PyQt is checking if the 
"self" on the bound instancemethod callback is a QObject in order to go down one 
path and otherwise go down another and failing to clear up this exception before 
returning control. I am using PyQt4 4.5 on OS X and trunk numpy, but I suspect 
any numpy will do. I am downloading 4.5.2 now to see if this has been fixed in 
the meantime, but I do not see anything in the changelog that looks promising.


import sys

from PyQt4 import QtGui
import numpy


class NotQObject(object):
     """ Definitely not a QObject.
     """
     def __init__(self, slider):
         self.slider = slider
         self.slider.valueChanged.connect(self.on_value_changed)

         a = numpy.arange(10).max()

     def on_value_changed(self, ival):
         print ival


def main():

     app = QtGui.QApplication(sys.argv)
     slider = QtGui.QSlider()
     nqo = NotQObject(slider)

     app.exec_()


if __name__ == '__main__':
     main()


-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco



More information about the PyQt mailing list