Qt6.2 Tutorial - 2. Introducing QWidgets, QMainWindow and QDialog in Qt

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, QMainWindowand QDialog, and compare their characteristics and uses.

QWidget: the basic building block

overview

QWidgetIs the base class for all user interface elements in Qt. Everything from buttons to text boxes, and even windows and dialog boxes are subclasses QWidgetof . QWidgetCan 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();
}

insert image description here

QMainWindow: a framework for building complex applications

overview

QMainWindowis 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();
}

insert image description here

QDialog: Interactive dialog

overview

QDialogis 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;
}

insert image description here

Compared

  • Universal : QWidgetis the most general and can be used as the basis for creating other controls or containers; QMainWindowspecific to the main window, with menus and toolbars; and QDialogis mainly used to create dialog boxes.
  • Complexity : QMainWindowUsually more complex than QWidgetand QDialogbecause it contains more components (such as menu bars, toolbars, status bars, and dock widgets).
  • Purpose : QDialogMainly used to obtain user input or display information; QMainWindowusually used as the main interface of the application; QWidgetcan be used for both, and is the basis of all controls.

Summarize

In Qt, QWidget, QMainWindowand QDialogare 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.

Guess you like

Origin blog.csdn.net/qq_43657810/article/details/131277111