What's the difference between QMainWindow, QWidget and QDialog?
Solution 1
A QWidget
is the base class for all drawable classes in Qt. Any QWidget
-based class can be shown as a window by showing it when it has no parent.
A QDialog
is based on QWidget
, but designed to be shown as a window. It will always appear in a window, and has functions to make it work well with common buttons on dialogs (accept, reject, etc.).
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. It does not have any built-in allowances for buttons like QDialog
does.
Solution 2
QWidget :is a base class for all other GUI elements in QtWidgets module. It can constitute a window by itself, or be part of a QLayout, or just a member of parent-child hierarchy.
QDialog :is usually used to - surprise! - display a temporary dialog when user input is required.
QMainWindow :is a convenience class that can be used as the main window of your application. It has some nice features built-in: a status bar, tool bars and a menu bar
zibib
Updated on November 11, 2020Comments
-
zibib over 3 years
What's the difference between QMainWindow, QWidget and QDialog?
-
Lilian A. Moraru over 11 yearsI searched for this question but that's not really the answer I was waiting for. In QtDesigner using a QMainWindow gives you a menu bar and a status bar. When you write the code yourself and use QMainWindow you don't have this things. Or your saying that you can do something like window.addToolBar("Toolbar"); and it will place it at a predefined place without any other headache?
-
Caleb Huitt - cjhuitt over 11 years@MoraruLilian: I don't actually use QMainWindow, so I don't know the particulars. As I said in my answer, I know it has places for them, so doing it in code might be as simple as you state.
-
Lilian A. Moraru over 11 years@Caleb_Huitt It does what I am saying. I wanted just clear up if that's what you are trying to say. Anyway, I would recommend always using QWidget instead of QMainWindow. You can get into problems, for example with setting another Layout. If you want to set a new layout in QMainWindow you would have to create a QWidget and set it as a central widget for QMainWin and just then in QWidget you could setLayout(QLayout *). While you could avoid additional headaches just by using QWidget. Sure, QMainWindow offers that easy setup of the ToolBar and StatusBar but you can do that yourself in QWidget...
-
ComradeJoecool over 5 yearsOne reason we are looking to use
QMainWindow
instead of just the base classQWidget
is thatQMainWindow
hasQDockWidget
support whereasQWidget
alone does not -
mLstudent33 about 4 yearsif I have a base class QMainWindow called calculator.ui, calculator.cpp, calculator.h, what does the constructor for derived class scientific_calculatror look like?
-
Caleb Huitt - cjhuitt about 4 years@mLstudent33 That sounds like a good question for documentation, and if that doesn't help, for asking here as a primary question rather than in the comments. One example (for Qt5) can be seen at doc.qt.io/qt-5/qtwidgets-mainwindows-application-example.html