PySide Python3 Properties problem



  • Hi! I'm new to Pyside and I was looking at the basic tutorials (http://qt-project.org/wiki/Using_Qt_Properties_in_PySide). I have found out, that the Properties does not work in Python3 as expected. Please take a look at:
    @class MyObject(QObject):
    def init(self,startval=42):
    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)@

    In Python3 you get:
    @<Property object at 0x2f39948>@
    as a result, and it should return 47.



  • I don't have the answer, but found the question interesting.

    Your example should be more complete. I found that Property is defined by PySide. So you should add to your example:

    @from PySide.QtCore import QObject, Property@

    But calling the standard Python property works:

    @pp = property(readPP, setPP) @

    I am left wondering what the documentation means "simultaneously behave both as Qt and Python properties"? What does "Qt property" give you?

    Could it have something to do with not embedding in a larger Qt application where signals are initialized?



  • This is what works for me:

    @
    from PySide.QtCore import QObject, Property

    class MyObject(QObject):
    def init(self,startval=42):
    self.ppval = startval

    def readPP(self):
        return self.ppval
    
    def setPP(self,val):
        self.ppval = val
    
    pp = Property(int, readPP, setPP)
    

    obj = MyObject()
    print(obj.readPP()) # Should print 42
    obj.setPP(47)
    print(obj.readPP()) # Should print 47

    @



  • Indn: aren't you missing the point: you should be able to access the value of a property by simply referring to the attribute 'obj.pp', without needing to call the getter method 'obj.readPP().' Its related to "Command Query Separation Principle": a query should not need call syntax (with '()') even if some computation (getter method) is involved.



  • This works. The parent QObject needs to be initialized (see 5th line).

    @
    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)
    @



  • You can also call the parent QObject with the super() method.

    @
    from PySide.QtCore import QObject, Property

    class MyObject(QObject):
    def init(self,startval=42):
    super().init()
    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)
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.