Qt for Python UsingQtProperties: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' [[Using Qt Properties in PySide Korean|한국어]]
[[Category:LanguageBindings::PySide]]<br />[[Category:Snippets]]


=Using Qt Properties in PySide=
'''English''' [[:Using_Qt_Properties_in_PySide_Korean|한국어]]


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.
= Using Qt Properties in PySide =
 
PySide provides a &lt;code&amp;gt;Property&amp;lt;/code&amp;gt; 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:


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 <span class="caps">PSEP</span> 103] ''[web.archive.org]''.
<code><br />from PySide.QtCore import QObject, Property
 
class MyObject(QObject):<br /> def ''init''(self,startval=42):<br /> QObject.''init''(self)<br /> self.ppval = startval
 
def readPP(self):<br /> return self.ppval
 
def setPP(self,val):<br /> self.ppval = val
 
pp = Property(int, readPP, setPP)
 
obj = MyObject()<br />obj.pp = 47<br />print (obj.pp)<br /></code>
 
The complete specification for PySide's property system is given in &quot;PSEP 103&amp;quot;:http://web.archive.org/web/20120423210319/http://www.pyside.org/docs/pseps/psep-0103.html.
 
== Properties in QML expressions ==


==Properties in <span class="caps">QML</span> 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:


If you are using properties of your objects in <span class="caps">QML</span> expressions, <span class="caps">QML</span> requires the property to be <span class="caps">NOTIFY</span>able. This can be done using a simple signal:
<code><br />class Person(QtCore.QObject):<br /> def ''init''(self, name):<br /> QtCore.QObject.''init''(self)<br /> self._person_name = name


Emit the signal when the data changes, and <span class="caps">QML</span> will automatically update all expressions depending on the value.
def _name(self):<br /> return self._person_name


===Categories:===
&amp;#64;QtCore.Signal<br /> def name_changed(self): pass


* [[:Category:LanguageBindings|LanguageBindings]]
name = QtCore.Property(unicode, _name, notify=name_changed)<br /></code>
** [[:Category:LanguageBindings::PySide|PySide]]
* [[:Category:snippets|snippets]]

Revision as of 09:03, 24 February 2015


English 한국어

Using Qt Properties in PySide

PySide provides a <code&gt;Property&lt;/code&gt; 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:

<br />from PySide.QtCore import QObject, Property

class MyObject(QObject):<br /> def ''init''(self,startval=42):<br /> QObject.''init''(self)<br /> self.ppval = startval

def readPP(self):<br /> return self.ppval

def setPP(self,val):<br /> self.ppval = val

pp = Property(int, readPP, setPP)

obj = MyObject()<br />obj.pp = 47<br />print (obj.pp)<br />

The complete specification for PySide's property system is given in "PSEP 103&quot;: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:

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

def _name(self):<br /> return self._person_name

&amp;#64;QtCore.Signal<br /> def name_changed(self): pass

name = QtCore.Property(unicode, _name, notify=name_changed)<br />