Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-1613

Unexpected behavior with inheritance and properties

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Out of scope
    • Icon: Not Evaluated Not Evaluated
    • None
    • 6.2.9
    • PySide
    • None

      The following code gives unexpected results
       

      import signal
      import sys
      from PySide6.QtCore import QCoreApplication, Property, QObject, Signal, Slot
      
      class Base(QObject):
          iChanged = Signal(int)
       
          def __init__(self, i, parent=None):
              super().__init__(parent)
              self._i = i
      
          def getI(self):
              return self._i
       
          def setI(self, i):
              if self._i != I:
                  self._i = i
                  self.iChanged.emit(i)
      
          i = Property(int, getI, setI, notify = iChanged)
      
      
      class Derived(Base):
      
          def__init__(self, i, parent=None):
              super().__init__(i, parent)
      
          def getI(self):
              return 2 * self._i
       
          # i = Property(int, getI) # Try with and without umcommenting...
      
      @Slot(int)
      def testBase(i):
          print(f'Base slot called with i = {i}')
      
      @Slot(int)
      def testDerived(i):
          print(f'Derived slot called with i = {i}')
      
      if __name__ == '__main__':
          app = QCoreApplication(sys.argv)
          base = Base(2)
          derived = Derived(2)
          print(f'base.i = {base.i}/{base.getI()}')
          print(f'derived.i = {derived.i}/{derived.getI()}')
          base.iChanged.connect(testBase)
          derived.iChanged.connect(testDerived)
          base.setI(4)
          derived.setI(5)
          print(f'derived.i = {derived.i}/{derived.getI()}')
          signal.signal(signal.SIGINT, signal.SIG_DFL)
          sys.exit(app.exec())

       
      The results, without declaring a 2nd Property in Derived, are:
      base.i = 2/2

      derived.i = 2/4

      Base slot called with i = 4

      Derived slot called with i = 5

      derived.i = 5/10

      Note that the Property getter calls the getI method of Base, not Derived.  If the redundant Property is declared in Derived, it fixes the derived instance so derived.i and derived.getI() give the same values, but doesn't seem like creating duplicate Properties is a good idea.

        1. pyside1613.py
          2 kB
          Friedemann Kleint
        2. test_inheritance.py
          1 kB
          Brett Stottlemyer
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            crmaurei Cristian Maureira-Fredes
            bstottle Brett Stottlemyer
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes