Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-1642

Declaring a Property as constant raises an exception

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • Not Evaluated
    • 6.1.2
    • 5.15.4, 6.1.1
    • PySide
    • None
    • All

    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

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              crmaurei Cristian Maureira-Fredes
              eyllanesc Edwin Christian Yllanes Cucho
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes