-3

I am new to Qt and this is probably a very stupid question. I am working on a Project with several sub-projects in Visual Studio (C++). The whole project has a single User-interface (UI), and for that, the Qt is used.

Among all the sub-projects, only 2 classes are inherited from QMainWindow.

As a newbie, I am wondering, which classes should inherit QMainWindow?

skm
  • 5,015
  • 8
  • 43
  • 104
  • Actually, no class must inherit from `QMainWindow`. You just could "plug" the widgets together (e.g. in `main()`. - Most of my little examples are done that way.) However, there might be a closer interdependency of `QMainWindow` contents (concerning widgets and the data which is handled by these widgets). To glue things together, it might make sense to derive `QMainWindow` and make all the interdepending stuff members of this class. Alternatively you could make another application class where `QMainWindow` is a member itself. – Scheff's Cat Sep 09 '19 at 08:47
  • ... It's a question of design - hard to justice with this less information you gave. ;-) – Scheff's Cat Sep 09 '19 at 08:49
  • 1
    [What's the difference between QMainWindow, QWidget and QDialog?](https://stackoverflow.com/questions/3298792/whats-the-difference-between-qmainwindow-qwidget-and-qdialog) – talamaki Sep 09 '19 at 09:36
  • @Murphy: I am not asking the difference. I want to know the situations when you should inherit from the `QMainWindow`. – skm Sep 09 '19 at 12:30
  • 1
    You haven't read the accepted (and only) answer, have you? "`QMainWindow` is designed around common needs for a main window to have. It has predefined places for a menu bar, a status bar, a toolbar, and other widgets." So you derive from it when you need this facilities. See also the [docs](https://doc.qt.io/archives/qt-5.5/qmainwindow.html#details): "A main window provides a framework for building an application's user interface." – Murphy Sep 09 '19 at 14:45

1 Answers1

3

Just like any other class, QMainWindow should be inherited when one needs the functionality it provides (or rather to customize that functionality). Specifically QMainWindow (vs. just QWidget) provides built-in menu bar, dockable toolbar(s), status bar, dockable subwidgets, a way to save and restore their states, and some other niceties one might want for a full GUI application. But any QWidget can be a top-level widget (the "main window" of an app) so there is no requirement to use QMainWindow as the top-level widget. If one doesn't need/want any of those extras, there's no reason to inherit/use QMainWindow.

Maxim Paperno
  • 4,485
  • 2
  • 18
  • 22