Skip to content

Use PY_OBJECT for signal signatures

Carlos Pascual requested to merge github/fork/cpascual/qt-pyobjects into develop

Some signals use object in their signatures, but this may be problematic when object is imported from builtins (futurize module), as in the following example:

from builtins import object  # <-- if this is uncommented, the emit fails in py2 with "argument 1 has unexpected type 'tuple'"

from taurus.external.qt import Qt, compat


class B(Qt.QPushButton):
    s = Qt.pyqtSignal(object) 
    # s = Qt.pyqtSignal(compat.PY_OBJECT)  # <-- using this instead of the previous line fixes the issue and is Qt-binding agnostic...

    def __init__(self):
        super(B, self).__init__()
        self.clicked.connect(self.on_click)
        self.s.connect(self.on_s)
        self.setText("CLICK ME")

    def on_click(self):
        # (!) this emit does not match the signature in py2 if object was imported from builtins
        self.s.emit((1,2,3))  

    def on_s(self, a):
        print(a)


if __name__ == '__main__':
    import sys
    app = Qt.QApplication([])
    w = B()
    w.resize(300,300)
    w.show()
sys.exit(app.exec_())

To avoid it, create a new variable PY_OBJECT in taurus.external.qt.compat wich takes value 'PyQt_PyObject' in PyQt or 'PyObject' in PySide/PySide2 and use it instead of object in all signatures.

Merge request reports