Description
By declaring a property as constant using the following code:
import sys from PySide2.QtCore import Property, QObject # from PyQt5.QtCore import pyqtProperty as Property, QObject class Foo(QObject): def __init__(self, parent=None): super().__init__(parent) self._foo_object = QObject() @Property(QObject, constant=True) def foo(self): return self._foo_object if __name__ == "__main__": foo = Foo()
Throw the exception:
Traceback (most recent call last): File "/home/qt_user/main.py", line 7, in <module> class Foo(QObject): File "/home/qt_user/main.py", line 13, in Foo def foo(self): TypeError: A constant property cannot have a WRITE method or a NOTIFY signal.
But if PyQt5 is used it does not generate this error.
A workaround is to implement a getter and declare the property without the decorator
import sys from PySide2.QtCore import Property, QObject class Foo(QObject): def __init__(self, parent=None): super().__init__(parent) self._foo_object = QObject() def get_foo(self): return self._foo_object foo = Property(QObject, fget=get_foo, constant=True) if __name__ == "__main__": foo = Foo()
But that introduces creating a getter that I think is unnecessary.
Attachments
Issue Links
- duplicates
-
PYSIDE-1426 Property decorator with constant=True generates TypeError
- Closed