34

The QMainWindow is the main window in a Qt application. So usually you'd have only one, but would it be possible at all to have multiple QMainWindow instances in your application?

I am working on integrating a Qt-based GUI application B into another Qt-based GUI application A. Both these applications have a QMainWindow, and I was considering as a first step to create a new QMainWindow that has both old QMainWindows on tabs. That way it would allow me to concentrate on wiring the backend of GUI B to the backend of A without having to change anything in the user interface itself. Is this a viable approach, or do you have other suggestions?

demonplus
  • 5,613
  • 12
  • 49
  • 68
andreas buykx
  • 12,608
  • 10
  • 62
  • 76
  • I am in a pretty much similar situation, where I need 2 QMainWindows. In case you have gone this route, do you have an answer to the caveat pointed out by @caleb-huitt-cjhuitt, that is what happens to the menu bar on a Mac? – S B Aug 05 '11 at 05:25

1 Answers1

43

You can have as many QMainWindow instances as you want. A QMainWindow is just a QWidget which provides a menu bar, toolbar, status bar and docking framework. But basically it is just a QWidget so you can have as many as you like.

Normally you only have a single QMainWindow for GUI design reasons since it can be confusing to have multiple main windows. However, when implementing applications like QtAssistant which can open a new instance of themselves then the one process has multiple main windows.

What you suggest should work. The window with the tabs would not need to be a QMainWindow unless you want to also want add a toolbar to that window as well.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
David Dibben
  • 18,460
  • 6
  • 41
  • 41
  • 4
    The only caveat I would add is that the menus might get a little funky if you were to port to OS X. They have some special handling to get the menus to work at the top of the screen, and I don't know how that would work with two main windows in one actual window. – Caleb Huitt - cjhuitt Nov 26 '08 at 15:49
  • 1
    Given that you can have multiple QMainWindow in application, could you use its docking framework inside a Mdi SubWindow? Would that be a good practice? – Anonymous Mar 19 '12 at 13:52
  • 1
    How this sub-QMainWindow(s) appears in main application window? How they are arranged: like MDI windows, overlap each other or need to place them in some container like tab widget or stacked widget, or they are stanalone - how? P.S. Found the answer here: http://stackoverflow.com/a/11860378/630169 and here https://forum.qt.io/topic/31841/qmainwindow-inside-a-widget-can-be-done-or-is-not-correct/7, need to use window->setWindowFlags(Qt::Widget); – Aleksey Kontsevich Mar 29 '16 at 01:08