Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. couldn't understand error "'__init__' method of object's base class not called"
Forum Updated to NodeBB v4.3 + New Features

couldn't understand error "'__init__' method of object's base class not called"

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 2.1k 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.
  • V Offline
    V Offline
    VoLinhTruc
    wrote on last edited by
    #1

    Hi friends, while using QUILoader, I meet this weird error that I couldn't find out the root cause that error.

    I created a class inheritant from QMainWindow to override the closeEvent() method like this

    class myQMainWindow(QMainWindow):
    	def __init__(self):
    		QMainWindow.__init__(self)
    	def closeEvent(self, event):
    	    event.ignore()
        # doing something else
    

    And this is my complete code

    import sys, os
    
    from PySide6.QtCore import Signal, Slot
    from PySide6.QtWidgets import QApplication, QMainWindow, QWidget
    from PySide6.QtCore import QMutex
    from PySide6.QtCore import QThread, QObject
    from PySide6.QtUiTools import QUiLoader
    from PySide6.QtCore import QFile, QIODevice
    
    
    class myQMainWindow(QMainWindow):
    	def __init__(self):
    		QMainWindow.__init__(self)
    	def closeEvent(self, event):
    	    event.ignore()
        # doing something else
    
    
    if __name__ == "__main__":
    
        app = QApplication(sys.argv)
    
    
        #  This block cause error "'__init__' method of object's base class (myQMainWindow) not called" 
        _loader = QUiLoader()
        file = QFile("xx.ui")
        file.open(QIODevice.ReadOnly)
        my_win = _loader.load(file)
        file.close()
        # my_win is PySide6.QtWidgets.QMainWindow
        print (repr(my_win))
    
    
        # # This block work fine
        # my_win = QMainWindow()
        # # my_win is also PySide6.QtWidgets.QMainWindow
        # print (repr(my_win))
    
    
        my_win.__class__ = myQMainWindow
        print (repr(my_win))
        my_win.show()
    
        sys.exit(app.exec())
    

    The error show up when I run the code above

    '__init__' method of object's base class (myQMainWindow) not called
    

    I checked that

    my_win = _loader.load(file)
    

    return PySide6.QtWidgets.QMainWindow to my_win

    I tried replacing

        #  This block cause error "'__init__' method of object's base class (myQMainWindow) not called" 
        _loader = QUiLoader()
        file = QFile("xx.ui")
        file.open(QIODevice.ReadOnly)
        my_win = _loader.load(file)
        file.close()
        # my_win is PySide6.QtWidgets.QMainWindow
        print (repr(my_win))
    

    by

        # This block work fine
        my_win = QMainWindow()
        # my_win is also PySide6.QtWidgets.QMainWindow
        print (repr(my_win))
    

    which also return PySide6.QtWidgets.QMainWindow to my_win
    and there are no error

    I can not understand why.

    If you guys experiment with this, please support.
    Thank you a lot in advance.

    ui file

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>Bico_QWindowThread_Sample_UI</class>
     <widget class="QMainWindow" name="Bico_QWindowThread_Sample_UI">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>516</width>
        <height>379</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>Bico_QWidgetThread_UI_Example</string>
      </property>
      <widget class="QWidget" name="centralwidget"/>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>516</width>
         <height>22</height>
        </rect>
       </property>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
     <slots>
      <slot>slot1()</slot>
     </slots>
    </ui>
    
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      Your UI file does not seem to contain anything that would trigger creating a myQMainWindow, and you are not using a customised QUiLoader that overrides createWidget() to return a myQMainWidnow when a QMainWindow was requested. So, you get a QMainWindow out of load().

      It look like you then try to retrospectively change the class of the returned object:

      my_win.__class__ = myQMainWindow
      

      which may, or may not be, valid in Python (no Python expert here). Since the object already exists and has been constructed/initialised the constructor will not be called again. My bet it is this line alone that generates the warning.

      V 1 Reply Last reply
      2
      • C ChrisW67

        Your UI file does not seem to contain anything that would trigger creating a myQMainWindow, and you are not using a customised QUiLoader that overrides createWidget() to return a myQMainWidnow when a QMainWindow was requested. So, you get a QMainWindow out of load().

        It look like you then try to retrospectively change the class of the returned object:

        my_win.__class__ = myQMainWindow
        

        which may, or may not be, valid in Python (no Python expert here). Since the object already exists and has been constructed/initialised the constructor will not be called again. My bet it is this line alone that generates the warning.

        V Offline
        V Offline
        VoLinhTruc
        wrote on last edited by
        #3

        @ChrisW67
        Thank you a lot, I solved the issue with createWidget

        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