Qt for Python UsingQtProperties: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
(Convert ExpressionEngine links)
Line 33: Line 33:
</code>
</code>


The complete specification for PySide's property system is given in "PSEP 103":http://web.archive.org/web/20120423210319/http://www.pyside.org/docs/pseps/psep-0103.html.
The complete specification for PySide's property system is given in [http://web.archive.org/web/20120423210319/http://www.pyside.org/docs/pseps/psep-0103.html PSEP 103].


== Properties in QML expressions ==
== Properties in QML expressions ==

Revision as of 15:48, 4 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

English 한국어

Using Qt Properties in PySide

PySide 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 PySide.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)

The complete specification for PySide's property system is given in PSEP 103.

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:

class Person(QtCore.QObject):
 def ''init''(self, name):
 QtCore.QObject.''init''(self)
 self._person_name = name

def _name(self):
 return self._person_name

&amp;#64;QtCore.Signal
 def name_changed(self): pass

name = QtCore.Property(unicode, _name, notify=name_changed)