What's the difference between QMainWindow, QWidget and QDialog?
3 Answers
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.

- 13,613
- 4
- 67
- 57

- 14,785
- 3
- 42
- 49
-
1I 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? – Lilian A. Moraru Oct 24 '12 at 21:56
-
1@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. – Caleb Huitt - cjhuitt Oct 25 '12 at 17:31
-
10@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... – Lilian A. Moraru Nov 05 '12 at 01:00
-
4One reason we are looking to use `QMainWindow` instead of just the base class `QWidget` is that `QMainWindow` has `QDockWidget` support whereas `QWidget` alone does not – ComradeJoecool Nov 09 '18 at 16:19
-
if I have a base class QMainWindow called calculator.ui, calculator.cpp, calculator.h, what does the constructor for derived class scientific_calculatror look like? – mLstudent33 May 30 '20 at 22:10
-
2@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 https://doc.qt.io/qt-5/qtwidgets-mainwindows-application-example.html – Caleb Huitt - cjhuitt Jun 01 '20 at 00:59
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

- 382
- 4
- 13
Widgets: Building Blocks of Your Interface
Widgets are like the LEGO blocks of your computer screen. They're the visual elements you interact with, such as buttons, sliders, input fields, and more. Widgets do two main jobs: they display information on the screen and let you do things like clicking or typing. On Windows, you might hear them called "controls." No matter the name, widgets make up the pieces of your app that users can see and touch.
Windows: Containers for Widgets
Imagine windows as frames that hold your widgets together. They're like the borders of a painting that keep everything in place. Windows come with titles and outlines and can be moved around on your screen. They're like the bosses of the widget world, because they manage how everything fits together. Depending on your computer's style (like Windows, KDE, GNOME), windows have certain rules about how they should look and behave.
Dialogs: Quick Info and Input Pop-Ups
Dialogs are like little sticky notes that your app can show you. They're pop-ups that appear when your program wants to tell you something or needs some information. Dialogs are temporary and usually have buttons like "OK" or "Cancel." They're great for showing messages or asking quick questions, but they're not meant to stay open for long.
QMainWindow: The Command Center
Now, let's talk about QMainWindow. It's like a special kind of window that's super powerful. It's often the main hub of your app, where you put your important stuff. QMainWindow has a title bar, toolbars, and areas for widgets. It's perfect for apps that need a central place to manage everything, like toolbars at the top and widgets for different parts of your app.
To sum it all up, widgets are your app's LEGO blocks, windows are the frames that hold them, dialogs are like sticky notes, and QMainWindow is your app's command center. By understanding these simple concepts, programmers can create interfaces that are both easy on the eyes and easy to use.

- 61
- 1
- 4