Qt for Python UsingQtProperties: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:LanguageBindings::PySide]] | [[Category:LanguageBindings::PySide]] | ||
[[Category:Snippets]] | |||
'''English''' [[:Using_Qt_Properties_in_PySide_Korean|한국어]] | '''English''' [[:Using_Qt_Properties_in_PySide_Korean|한국어]] | ||
Line 5: | Line 6: | ||
= Using Qt Properties in PySide = | = Using Qt Properties in PySide = | ||
PySide provides a | PySide provides a <code>Property</code> 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: | A short example illustrating defining and accessing a Qt property from Python is given below: | ||
<code> | <code> | ||
from PySide.QtCore import QObject, Property | |||
class MyObject(QObject): | class MyObject(QObject): | ||
def ''init''(self,startval=42): | |||
QObject.''init''(self) | |||
self.ppval = startval | |||
def readPP(self): | def readPP(self): | ||
return self.ppval | |||
def setPP(self,val): | def setPP(self,val): | ||
self.ppval = val | |||
pp = Property(int, readPP, setPP) | pp = Property(int, readPP, setPP) | ||
obj = MyObject() | obj = MyObject() | ||
obj.pp = 47 | |||
print (obj.pp) | |||
</code> | |||
The complete specification for PySide's property system is given in | 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. | ||
== Properties in QML expressions == | == Properties in QML expressions == | ||
Line 27: | Line 37: | ||
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: | 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: | ||
<code> | <code> | ||
class Person(QtCore.QObject): | |||
def ''init''(self, name): | |||
QtCore.QObject.''init''(self) | |||
self._person_name = name | |||
def _name(self): | def _name(self): | ||
return self._person_name | |||
&#64;QtCore.Signal | &#64;QtCore.Signal | ||
def name_changed(self): pass | |||
name = QtCore.Property(unicode, _name, notify=name_changed) | name = QtCore.Property(unicode, _name, notify=name_changed) | ||
</code> |
Revision as of 09:33, 25 February 2015
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":http://web.archive.org/web/20120423210319/http://www.pyside.org/docs/pseps/psep-0103.html.
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
&#64;QtCore.Signal
def name_changed(self): pass
name = QtCore.Property(unicode, _name, notify=name_changed)