Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.
Qt for Python UsingQtProperties: Difference between revisions
Jump to navigation
Jump to search
Wolf Bublitz (talk | contribs) (fixed identation of getters, setters, signals and properties) |
AutoSpider (talk | contribs) (Move Category:QtForPython -> Category:Qt for Python) |
||
Line 1: | Line 1: | ||
[[Category: | [[Category:Qt for Python]] | ||
PySide2 provides a ''Property'' function which allows for declaring properties that simultaneously behave both as Qt and Python properties, and have their setters and getters defined as Python functions. | PySide2 provides a ''Property'' function which allows for declaring properties that simultaneously behave both as Qt and Python properties, and have their setters and getters defined as Python functions. |
Revision as of 15:19, 27 May 2018
PySide2 provides a Property function which allows for declaring properties that simultaneously behave both as Qt and Python properties, and have their setters and getters defined as Python functions.
A short example illustrating defining and accessing a Qt property from Python is given below:
from PySide2.QtCore import QObject, Property
class MyObject(QObject):
def __init__(self,startval=42):
QObject.__init__(self)
self.ppval = startval
def readPP(self):
return self.ppval
def setPP(self,val):
self.ppval = val
pp = Property(int, readPP, setPP)
obj = MyObject()
obj.pp = 47
print(obj.pp)
Properties in QML expressions
If you are using properties of your objects in QML expressions, QML requires the property to be NOTIFYable. This can be done using a simple signal:
from PySide2.QtCore import QObject, Signal, Property
class Person(QObject):
def __init__(self, name):
QObject.__init__(self)
self._person_name = name
def _name(self):
return self._person_name
@Signal
def name_changed(self):
pass
name = Property(str, _name, notify=name_changed)