Important: Please read the Qt Code of Conduct -

QMainWindow inside a widget, can be done or is not correct?

  • Hi all

    I would like to "embed" a QMainWindow inside a widget for allow me to use the QDocWidget feature only inside the widget itself. Looking around I found some tricks it seem allow to make it (not tested yet). Basically the trick consist in set the widget flag to the QMainWindow derived object as explained here:

    However, before start implementation, my doubt is about the correctness to make such operation. I mean, is correct to use an object designed to be the top level window class inside a child window? This case will have two QMainWindow objects. Could this "redoundace" cause some unexpeced bugs?

    Thank you for your help

  • Hi,
    A QMainWindow is also just a very complex widget, so yes, you could use multiple QMainWindow widgets. I don't think I would recommend it. Never given it any thought ;-)

  • Hi

    Thank you for reply

    Than there is no problem but why you would not reccoment make such thing? Is the doubt I have. Could be an incorrect use of this class?

  • I am writing a program which is using several QMainWindows as pages of a QTabWidget, since I wanted to have the features of QDockWidget available. It works perfectly.

    As stated, QMainWindow is just a subclass of QWidget, so you can do with it whatever you like. If you look at the classes functions, you'll notice that none of them actually need the QMainWindow to be the top-level window. There is merely additional functionality, it is mostly about Toolbars, Menubars and Dockwidgets.

    Also, the only other way to use dockwidgets would be very complicated and probably somewhat error-prone. You would have to rewrite half of QMainWindow in a standard QWidget-subclass. And I am not sure if you could gain anything from that, at all.

  • Hi thEClaw

    Thank you for your reply, very useful. By the way you used the trick suggested in the link I posted before for have QMainWindows working as child window?

    Thank you

  • I am not sure which "trick" you mean. Setting the QMainWindows flags to Qt::Widget instead of Qt::Window? I didn't do that.

  • Hi

    Based to the advises explained in the link above the proble is the following:

    _QMainWindow sets its window type to Qt::Window so that it will be in an independent window even if it has a parent (you can confirm this by calling show() on your QMainWindow, and you will see it in its own window). Try adding the line


    after you construct the QMainWindow._

    Anyway if it work for you this is not very important. I was sure it can be done, my doub are mainly connected to the "correctness" using this class in such way.

  • Moderators

    [quote author="Suppaman" date="1378980192"] I was sure it can be done, my doub are mainly connected to the "correctness" using this class in such way.[/quote]
    There is nothing wrong in embedding QMainWindow as a child-widget. Although the name may be a bit confusing though. But it's a simple widget which has features which are common as a starting point of your application, the main window.

  • OK, thank you very much for the clarification. My doub is disappeared now! :)

Log in to reply