Qt for Python UsingQtProperties: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Rename category "LanguageBindings::PySide" -> "PySide")
No edit summary
Line 1: Line 1:
[[Category:QtForPython]]


 
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.
[[Category:PySide]]
[[Category:Snippets]]
 
'''English''' [[:Using_Qt_Properties_in_PySide_Korean|한국어]]
 
 
 
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:
A short example illustrating defining and accessing a Qt property from Python is given below:


<code>
<syntaxhighlight lang="python" line='line'>
from PySide.QtCore import QObject, Property
from PySide2.QtCore import QObject, Property                                                      
 
                                                                                                   
class MyObject(QObject):
class MyObject(QObject):                                                                          
def ''init''(self,startval=42):
    def __init__(self,startval=42):                                                                
QObject.''init''(self)
        QObject.__init__(self)                                                                    
self.ppval = startval
        self.ppval = startval                                                                      
 
                                                                                                   
def readPP(self):
def readPP(self):                                                                                  
return self.ppval
    return self.ppval                                                                              
 
                                                                                                   
def setPP(self,val):
def setPP(self,val):                                                                              
self.ppval = val
    self.ppval = val                                                                              
 
                                                                                                   
pp = Property(int, readPP, setPP)
pp = Property(int, readPP, setPP)                                                                  
 
obj = MyObject()                                                                                  
obj = MyObject()
obj.pp = 47                                                                                        
obj.pp = 47
print(obj.pp)  
print (obj.pp)
</syntaxhighlight>
</code>
 
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 ==
Line 39: Line 29:
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>
<syntaxhighlight lang="python" line='line'>
class Person(QtCore.QObject):
from PySide2.QtCore import QObject, Signal, Property
def ''init''(self, name):
 
QtCore.QObject.''init''(self)
class Person(QObject):
self._person_name = name
    def __init__(self, name):
        QObject.__init__(self)
        self._person_name = name


def _name(self):
def _name(self):
return self._person_name
    return self._person_name


@QtCore.Signal
@Signal
def name_changed(self): pass
def name_changed(self):
    pass


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

Revision as of 12:58, 18 April 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)