Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. PySide Python3 Properties problem
Qt 6.12 LTS is out! See what's new in the release blog

PySide Python3 Properties problem

Scheduled Pinned Locked Moved Language Bindings
6 Posts 3 Posters 4.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lndn
        wrote on last edited by
        #3

        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

        @

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lndn
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lndn
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt
              • Unsolved