couldn't understand error "'__init__' method of object's base class not called"
-
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 errorI 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>
-
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.