Qt for Python UsingQtProperties: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Introduced possibility of constant properties)
 
(12 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[[Category:LanguageBindings::PySide]]<br />[[Category:Snippets]]
[[Category:Qt for Python]]


'''English''' [[:Using_Qt_Properties_in_PySide_Korean|한국어]]
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.
 
= 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:


<code><br />from PySide.QtCore import QObject, Property
<syntaxhighlight lang="python" line='line'>
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)
</syntaxhighlight>


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


def readPP(self):<br /> return self.ppval
If you are using properties of your objects in QML expressions, QML requires the property to be NOTIFYable or constant. If the value of the property changes over time, you should apply NOTIFYable using a simple signal:


def setPP(self,val):<br /> self.ppval = val
<syntaxhighlight lang="python" line='line'>
from PySide2.QtCore import QObject, Signal, Property


pp = Property(int, readPP, setPP)
class Person(QObject):
    def __init__(self, name):
        QObject.__init__(self)
        self._person_name = name


obj = MyObject()<br />obj.pp = 47<br />print (obj.pp)<br /></code>
    def _name(self):
        return self._person_name


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.
    @Signal
    def name_changed(self):
        pass


== Properties in QML expressions ==
    name = Property(str, _name, notify=name_changed)
</syntaxhighlight>


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:
You can use constant if your value is immutable. Use it as follows:


<code><br />class Person(QtCore.QObject):<br /> def ''init''(self, name):<br /> QtCore.QObject.''init''(self)<br /> self._person_name = name
<syntaxhighlight lang="python" line='line'>
from PySide2.QtCore import QObject, Signal, Property


def _name(self):<br /> return self._person_name
class Person(QObject):
    def __init__(self, name):
        QObject.__init__(self)
        self._person_name = name


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


name = QtCore.Property(unicode, _name, notify=name_changed)<br /></code>
    name = Property(str, _name, constant=True)
</syntaxhighlight>

Latest revision as of 16:48, 17 March 2020


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 or constant. If the value of the property changes over time, you should apply NOTIFYable 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)

You can use constant if your value is immutable. Use it as follows:

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

    name = Property(str, _name, constant=True)