Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. In PySide6, why assignment before super().__init__() might lead to abnormal result?
QtWS25 Last Chance

In PySide6, why assignment before super().__init__() might lead to abnormal result?

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 354 Views
  • 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.
  • I Offline
    I Offline
    isaacchangwang
    wrote on last edited by
    #1

    Class A is derived from QObject, and class B is derived from A. In the GUI, when the OK button is clicked, the method B.log() is invoked, and it prints 'Hi' in the console. Here is the code:

    main.py

    import sys
    from PySide6.QtCore import QObject, QTimer, QUrl, Slot
    from PySide6.QtGui import QGuiApplication
    from PySide6.QtQml import QmlElement, QQmlApplicationEngine
    
    QML_IMPORT_NAME = 'PyLib'
    QML_IMPORT_MAJOR_VERSION = 1
    
    
    class A(QObject):
        def __init__(self):
            super().__init__()
    
    
    @QmlElement
    class B(A):
        def __init__(self):
            super().__init__()
    
        @Slot()
        def log(self):
            print('Hi')
    
    
    def main():
        app = QGuiApplication(sys.argv)
        engine = QQmlApplicationEngine()
        engine.load(QUrl('main.qml'))
        if not engine.rootObjects():
            sys.exit(-1)
        sys.exit(app.exec())
    
    
    if __name__ == '__main__':
        main()
    

    main.qml

    import QtQuick
    import QtQuick.Controls
    import PyLib
    
    ApplicationWindow {
        height: 100
        width: 100
        visible: true
    
        Button {
            text: "OK"
            onClicked: b.log()
        }
    
        B {
            id: b
        }
    }
    

    Then I find something I cannot explain:

    1. If I set an instance variable to a QTimer object before super().init() in the init() method of class B, i.e.,
    @QmlElement
    class B(A):
        def __init__(self):
            self.t = QTimer()
            super().__init__()
    
        @Slot()
        def log(self):
            print('Hi')
    

    when the OK button is clicked, no 'Hi' is printed. However, if I set the instance variable to an int or a string, e.g.,

    @QmlElement
    class B(A):
        def __init__(self):
            self.t = 1
            super().__init__()
    
        @Slot()
        def log(self):
            print('Hi')
    

    when the OK button is clicked, 'Hi' is printed.

    1. If a QTimer object is assigned to a local variable instead of an instance variable, i.e.,
    @QmlElement
    class B(A):
        def __init__(self):
            t = QTimer()
            super().__init__()
    
        @Slot()
        def log(self):
            print('Hi')
    

    the GUI window won't be shown.

    It seems super().init() must be called first in the initialization method, otherwise it might lead to abnormal result. Is that true?

    JonBJ 1 Reply Last reply
    0
    • I isaacchangwang

      Class A is derived from QObject, and class B is derived from A. In the GUI, when the OK button is clicked, the method B.log() is invoked, and it prints 'Hi' in the console. Here is the code:

      main.py

      import sys
      from PySide6.QtCore import QObject, QTimer, QUrl, Slot
      from PySide6.QtGui import QGuiApplication
      from PySide6.QtQml import QmlElement, QQmlApplicationEngine
      
      QML_IMPORT_NAME = 'PyLib'
      QML_IMPORT_MAJOR_VERSION = 1
      
      
      class A(QObject):
          def __init__(self):
              super().__init__()
      
      
      @QmlElement
      class B(A):
          def __init__(self):
              super().__init__()
      
          @Slot()
          def log(self):
              print('Hi')
      
      
      def main():
          app = QGuiApplication(sys.argv)
          engine = QQmlApplicationEngine()
          engine.load(QUrl('main.qml'))
          if not engine.rootObjects():
              sys.exit(-1)
          sys.exit(app.exec())
      
      
      if __name__ == '__main__':
          main()
      

      main.qml

      import QtQuick
      import QtQuick.Controls
      import PyLib
      
      ApplicationWindow {
          height: 100
          width: 100
          visible: true
      
          Button {
              text: "OK"
              onClicked: b.log()
          }
      
          B {
              id: b
          }
      }
      

      Then I find something I cannot explain:

      1. If I set an instance variable to a QTimer object before super().init() in the init() method of class B, i.e.,
      @QmlElement
      class B(A):
          def __init__(self):
              self.t = QTimer()
              super().__init__()
      
          @Slot()
          def log(self):
              print('Hi')
      

      when the OK button is clicked, no 'Hi' is printed. However, if I set the instance variable to an int or a string, e.g.,

      @QmlElement
      class B(A):
          def __init__(self):
              self.t = 1
              super().__init__()
      
          @Slot()
          def log(self):
              print('Hi')
      

      when the OK button is clicked, 'Hi' is printed.

      1. If a QTimer object is assigned to a local variable instead of an instance variable, i.e.,
      @QmlElement
      class B(A):
          def __init__(self):
              t = QTimer()
              super().__init__()
      
          @Slot()
          def log(self):
              print('Hi')
      

      the GUI window won't be shown.

      It seems super().init() must be called first in the initialization method, otherwise it might lead to abnormal result. Is that true?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @isaacchangwang said in In PySide6, why assignment before super().__init__() might lead to abnormal result?:

      It seems super().init() must be called first in the initialization method, otherwise it might lead to abnormal result. Is that true?

      I would always expect to have to call the base super().__init__() as the first statement in any derived class, whether Qt/QObject or not. Putting a QObject/QTimer into a derived class as a member before it has been initialised does not sound like a good idea.

      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 Extensions
      • Unsolved