January 17, 2013

danilo2 danilo2
Lab Rat
7 posts

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:

  1. class MyObject(QObject):
  2.     def __init__(self,startval=42):
  3.         self.ppval = startval
  4.  
  5.     def readPP(self):
  6.         return self.ppval
  7.  
  8.     def setPP(self,val):
  9.         self.ppval = val
  10.  
  11.     pp = Property(int, readPP, setPP)
  12.  
  13. obj = MyObject()
  14. obj.pp = 47
  15. print (obj.pp)

In Python3 you get:

  1. <Property object at 0x2f39948>

as a result, and it should return 47.

5 replies

February 10, 2013

bootchk bootchk
Hobby Entomologist
141 posts

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:

  1. from PySide.QtCore import QObject, Property

But calling the standard Python property works:

  1. 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?

March 19, 2013

lndn lndn
Lab Rat
16 posts

This is what works for me:

  1. from PySide.QtCore import QObject, Property
  2.  
  3. class MyObject(QObject):
  4.     def __init__(self,startval=42):
  5.         self.ppval = startval
  6.  
  7.     def readPP(self):
  8.         return self.ppval
  9.  
  10.     def setPP(self,val):
  11.         self.ppval = val
  12.  
  13.     pp = Property(int, readPP, setPP)
  14.  
  15. obj = MyObject()
  16. print(obj.readPP()) # Should print 42
  17. obj.setPP(47)
  18. print(obj.readPP()) # Should print 47

March 19, 2013

bootchk bootchk
Hobby Entomologist
141 posts

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.

March 20, 2013

lndn lndn
Lab Rat
16 posts

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

  1. from PySide.QtCore import QObject, Property
  2.  
  3. class MyObject(QObject):
  4.     def __init__(self,startval=42):
  5.         QObject.__init__(self)
  6.         self.ppval = startval
  7.  
  8.     def readPP(self):
  9.         return self.ppval
  10.  
  11.     def setPP(self,val):
  12.         self.ppval = val
  13.  
  14.     pp = Property(int, readPP, setPP)
  15.  
  16. obj = MyObject()
  17. obj.pp = 47
  18. print (obj.pp)

March 20, 2013

lndn lndn
Lab Rat
16 posts

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

  1. from PySide.QtCore import QObject, Property
  2.  
  3. class MyObject(QObject):
  4.     def __init__(self,startval=42):
  5.         super().__init__()
  6.         self.ppval = startval
  7.  
  8.     def readPP(self):
  9.         return self.ppval
  10.  
  11.     def setPP(self,val):
  12.         self.ppval = val
  13.  
  14.     pp = Property(int, readPP, setPP)
  15.  
  16. obj = MyObject()
  17. obj.pp = 47
  18. print (obj.pp)

 
  ‹‹ integration of PyQt/PySide issue      Ubuntu: Failed update of PySide ››

You must log in to post a reply. Not a member yet? Register here!