Qt Designer Cannot Promote QMainWindow

I do not understand anything about Qt Designer . I have been working with it for a while, and yet I always edit .ui files manually.

As you can see from each Qt tutorial, one of the first things you do when creating a GUI is a subclass of QMainWindow , for example, to intercept and override the closeEvent() function.

However, in Qt Designer it is not possible to subclass ("promote") QMainWindow - you can do this with many other classes, but not with this.

What I usually do is edit the .ui file and change:

 <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> 

by:

 < widget class="MyMainWindow" name="MainWindow"> 

So, my code can subclass QMainWindow correctly in my code:

 class MyMainWindow(QMainWindow, object): ''' I can redefine closeEvent here! ''' 

Obviously, this is strange - and I probably missed something about the right way to do this with Designer. But I can not find any explanation.

Why is this not possible in a subclass of QMainWindow in Designer, or how to structure my application differently to avoid its need?

thanks

+7
source share
2 answers

Usually you do not need to promote the root element of the user interface tree. If you correctly initialize your class using a UI file, you can override closeEvent without it. This answer shows how to do it right.

+4
source

I know this is old. I somehow ran into the same problem. I want to use docking functions in an additional window. When I tried to promote QMinWindow, I do not see it on the list. Then I tried using QWidget in the drop-down list, it magically passed.

Only my experience.

+2
source

All Articles