The Property function lets you declare properties that behave both as Qt and Python properties, and have their getters and setters defined as Python functions.
They are equivalent to the
Q_PROPERTY macro in the Qt Docs.
Here is an example that illustrates how to use this function:
1 from PySide2.QtCore import QObject, Property 2 3 class MyObject(QObject): 4 def __init__(self, startval=42): 5 QObject.__init__(self) 6 self.ppval = startval 7 8 def readPP(self): 9 return self.ppval 10 11 def setPP(self, val): 12 self.ppval = val 13 14 pp = Property(int, readPP, setPP) 15 16 obj = MyObject() 17 obj.pp = 47 18 print(obj.pp)
The full options for
QtCore.Property can be found with
Property(self, type: type, fget: Optional[Callable] = None, fset: Optional[Callable] = None, freset: Optional[Callable] = None, fdel: Optional[Callable] = None, doc: str = '', notify: Optional[Callable] = None, designable: bool = True, scriptable: bool = True, stored: bool = True, user: bool = False, constant: bool = False, final: bool = False) -> PySide2.QtCore.Property
fget``and ``fset are used.
Properties compared with Python properties¶
Python has property objects very similar to
Despite the fact that the latter has an extra
freset function, the usage
of properties is almost the same. The main difference is that
In the above example, the following lines would be equivalent properties:
pp = QtCore.Property(int, readPP, setPP) # PySide version pp = property(readPP, setPP) # Python version
As you know from the Python Docs,
Python allows to break the property
creation into multiple steps, using the decorator syntax. We can do this in
PySide as well:
1 from PySide2.QtCore import QObject, Property 2 3 class MyObject(QObject): 4 def __init__(self, startval=42): 5 QObject.__init__(self) 6 self.ppval = startval 7 8 @Property(int) 9 def pp(self): 10 return self.ppval 11 12 @pp.setter 13 def pp(self, val): 14 self.ppval = val 15 16 obj = MyObject() 17 obj.pp = 47 18 print(obj.pp)
Please be careful here: The two
Python functions have the same name, intentionally.
This is needed to let
Python know that these functions belong to the same property.
Properties in QML expressions¶
If you are using properties of your objects in QML expressions, QML requires that the property changes are notified. Here is an example illustrating how to do this:
1 from PySide2.QtCore import QObject, Signal, Property 2 3 class Person(QObject): 4 def __init__(self, name): 5 QObject.__init__(self) 6 self._person_name = name 7 8 def _name(self): 9 return self._person_name 10 11 @Signal 12 def name_changed(self): 13 pass 14 15 name = Property(str, _name, notify=name_changed)
© 2021 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.