Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Designer can't promote QMainWindow

There is something I do not understand about Qt Designer. I have been working with it for a while now, and still I always end up editing the .ui files manually.

As it can be seen in every single tutorial on Qt, one of the very first thing you do when you create a GUI is to subclass QMainWindow - in order to catch and redefine the closeEvent() function, for example.

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

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 that my code can subclass QMainWindow properly in my code:

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

Obviously, this is weird - and I probably miss something about the right way to do it with Designer. But I cannot find any explanation.

Why is it not possible to subclass QMainWindow in Designer, or how to structure my application differently to avoid the need for it?

Thanks

like image 476
maxime-esa Avatar asked Jun 10 '13 14:06

maxime-esa


People also ask

How to create GUI in Qt Designer?

Creating a Dialog GUI To create a custom dialog with Qt Designer, select the appropriate template for the dialog from the New Form dialog. Drag and drop the required widgets onto the form, lay out them correctly, and save the form in a . ui file for later use in your application.

Does Qt designer work with C++?

Qt Designer UI files represent the widget tree of the form in XML format. The forms can be processed: At compile time, which means that forms are converted to C++ code that can be compiled.


2 Answers

You generally don't need to promote the root element of UI tree. If you properly initialize your class using UI file, you can redefine closeEvent without it. This answer shows how to do it properly.

like image 200
Pavel Strakhov Avatar answered Sep 21 '22 04:09

Pavel Strakhov


I know this is old. I somehow ran into the same problem. I want to use docking features in a sub window. When I tried to promote the QMinWindow, I don't see it in the list. Then I tried using QWidget in the drop down list, it magically went through.

Just my experience.

like image 27
m. c. Avatar answered Sep 18 '22 04:09

m. c.