A detailed introduction to QWidgets, QMainWindow and QDialog in Qt
Qt is a popular cross-platform C++ GUI library. In Qt, the basic unit for creating a graphical user interface is Widget. This article will detail three important Widget classes: QWidget
, QMainWindow
and QDialog
, and compare their characteristics and uses.
QWidget: the basic building block
overview
QWidget
Is the base class for all user interface elements in Qt. Everything from buttons to text boxes, and even windows and dialog boxes are subclasses QWidget
of . QWidget
Can be a visual element or serve as a container for other visual elements.
main features
- Flexibility : Can be an individual control, such as a button, or a container for other controls.
- Customizable : You can set the size, style, background color, etc. of the QWidget.
- Event Handling : Supports handling of various events (such as mouse click and keyboard input).
example
#include <QApplication>
#include <QWidget>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("Basic QWidget Example");
window.resize(500, 300);
window.show();
return app.exec();
}
QMainWindow: a framework for building complex applications
overview
QMainWindow
is designed for standard applications that require a menu bar, toolbar, status bar, and central widget. It is usually used as the main window of the application.
main features
- Organizational Structure : Provides a central window that typically contains the main content of the application, with surrounding menu bars, toolbars, and status bars.
- Dock Widgets : Allows users to customize the window layout and organize content through draggable widgets.
example
#include <QApplication>
#include <QMainWindow>
#include <QLabel>
#include <QStatusBar>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QMainWindow mainWindow;
mainWindow.setWindowTitle("QMainWindow Example");
QLabel *centralLabel = new QLabel("Central Widget");
mainWindow.setCentralWidget(centralLabel);
mainWindow.statusBar()->showMessage("Status Bar Message");
mainWindow.show();
mainWindow.resize(500, 300);
return app.exec();
}
QDialog: Interactive dialog
overview
QDialog
is the class used to create dialogs. Dialog boxes are a special type of window that are typically used for short-lived tasks and brief communications. Users can enter or select information through dialog boxes.
main features
- Modal and non-modal : Dialogs can be modal (preventing the user from interacting with the parent window) or non-modal.
- Standard button : `QDialog
` Can contain standard buttons such as OK and Cancel.
example
#include <QApplication>
#include <QDialog>
#include <QPushButton>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QDialog dialog;
dialog.setWindowTitle("QDialog Example");
QPushButton *button = new QPushButton("Close", &dialog);
QObject::connect(button, &QPushButton::clicked, &dialog, &QDialog::accept);
dialog.exec();
return 0;
}
Compared
- Universal :
QWidget
is the most general and can be used as the basis for creating other controls or containers;QMainWindow
specific to the main window, with menus and toolbars; andQDialog
is mainly used to create dialog boxes. - Complexity :
QMainWindow
Usually more complex thanQWidget
andQDialog
because it contains more components (such as menu bars, toolbars, status bars, and dock widgets). - Purpose :
QDialog
Mainly used to obtain user input or display information;QMainWindow
usually used as the main interface of the application;QWidget
can be used for both, and is the basis of all controls.
Summarize
In Qt, QWidget
, QMainWindow
and QDialog
are important parts of building a graphical user interface. Which one to use depends on your specific needs: if you need a complex main interface, use QMainWindow
; if you need simple input and information display, use QDialog
; for other general purpose and custom controls, use QWidget
.