QT basic teaching (QMainWindow)


Preface

We have all learned in QWidget before, so today I will explain QMainWindow to you.

1. Introduction to QMainWindow

QMainWindow is an important window class provided by the Qt framework and is used to create the main window with a standard application layout. It provides some common features such as menu bar, toolbar, status bar, and central widget area for easy application organization and layout. The following are some features and common components of QMainWindow:

1.主窗口特点:

QMainWindow inherits from QWidget and provides a predefined layout that divides the window into menu bar, toolbar, status bar and central widget area.
The main window has the appearance of a top-level window and can contain other controls and layouts.

2.菜单栏和菜单:

QMainWindow has a predefined menu bar (QMenuBar) that can be used to create an application's multi-level menu structure.
You can add a menu (QMenu) using the addMenu() method of QMenuBar.
In QMenu, you can add menu items (QAction) and submenus (QMenu).

3.工具栏:

QMainWindow can contain one or more toolbars (QToolBar), which are used to place shortcut buttons for commonly used operations.
Toolbars can be added to the main window using the addToolBar() method.
Buttons in the toolbar can be bound to specific operations to facilitate quick operations for users.

4.状态栏:

QMainWindow has a predefined status bar (QStatusBar) for displaying the application's status information.
You can obtain the status bar object using the statusBar() method and display text messages through the showMessage() method.

5.中央部件区域:

The central widget area of ​​QMainWindow is the part used to place the main content of the application and can contain other QWidgets or custom QWidget derived classes.
The central widget can be set to a specific QWidget using the setCentralWidget() method.

QMainWindow提供了一个功能丰富的主窗口,方便创建具有标准布局的应用程序。它集成了菜单栏、工具栏、状态栏和中央部件区域,使得应用程序开发更加简单和高效。可以根据应用程序的需求,灵活地使用这些组件来设计和布局主窗口的界面。

Insert image description here
Insert image description here

2. Code examples

When we write a simple QMainWindow example using Qt, here is a basic code example:

#include <QApplication>
#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>
#include <QToolBar>
#include <QStatusBar>
#include <QAction>

int main(int argc, char *argv[]) {
    
    
    QApplication app(argc, argv);

    QMainWindow mainWindow;
    mainWindow.setWindowTitle("QMainWindow Example");

    // 创建菜单栏和菜单
    QMenuBar *menuBar = mainWindow.menuBar();
    QMenu *fileMenu = menuBar->addMenu("File");

    // 创建菜单项
    QAction *openAction = fileMenu->addAction("Open");
    QAction *saveAction = fileMenu->addAction("Save");

    // 创建工具栏
    QToolBar *toolBar = mainWindow.addToolBar("Toolbar");
    toolBar->addAction(openAction);
    toolBar->addAction(saveAction);

    // 创建状态栏
    QStatusBar *statusBar = mainWindow.statusBar();
    statusBar->showMessage("Ready");

    // 创建中央部件
    QWidget *centralWidget = new QWidget(&mainWindow);
    mainWindow.setCentralWidget(centralWidget);

    mainWindow.show();

    return app.exec();
}

operation result:

Insert image description here

3. Advanced usage of QMainWindow

1. Customize the toolbar: You can customize the appearance and functionality of the toolbar by adding custom QActions on the toolbar. For example, you can add custom buttons, separators, and other controls, as well as define actions associated with them.

2.Dock window: QMainWindow supports adding QDockWidget to it to create dockable child windows. Dock windows can be docked at the edge of the main window and can be collapsed, hidden and rearranged. This is useful for creating complex interfaces with customizable layouts and containers.

3. Multiple document interface (MDI): If your application needs to support multiple document interfaces, QMainWindow provides QMdiArea and QMdiSubWindow to implement multi-document interfaces. You can create and manage multiple subwindows in QMdiArea, and each subwindow can display different document content.

4. Other layouts: In addition to the central component, QMainWindow also supports adding QWidget components in other locations, such as left, right, top or bottom layout. You can use QVBoxLayout, QHBoxLayout, or other layout managers to manage the position and size of these widgets.

5. Shortcut keys and actions: QMainWindow allows you to define shortcut keys and actions for menu items, toolbar buttons, and other controls. You can implement user interaction with your application by setting QAction's shortcut keys and signal-slot connections.

#include <QApplication>
#include <QMainWindow>
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
#include <QAction>
#include <QDockWidget>
#include <QMdiArea>
#include <QMdiSubWindow>
#include <QVBoxLayout>
#include <QLabel>

int main(int argc, char *argv[]) {
    
    
    QApplication app(argc, argv);

    QMainWindow mainWindow;
    mainWindow.setWindowTitle("QMainWindow Advanced Example");

    // 创建菜单栏和菜单
    QMenuBar *menuBar = mainWindow.menuBar();
    QMenu *fileMenu = menuBar->addMenu("File");

    QAction *openAction = fileMenu->addAction("Open");
    QAction *saveAction = fileMenu->addAction("Save");

    // 创建工具栏
    QToolBar *toolBar = mainWindow.addToolBar("Toolbar");
    toolBar->addAction(openAction);
    toolBar->addAction(saveAction);

    // 创建状态栏
    QStatusBar *statusBar = mainWindow.statusBar();
    statusBar->showMessage("Ready");

    // 创建中央部件
    QWidget *centralWidget = new QWidget(&mainWindow);
    QVBoxLayout *layout = new QVBoxLayout(centralWidget);
    QLabel *label = new QLabel("Central Widget Content");
    layout->addWidget(label);

    mainWindow.setCentralWidget(centralWidget);

    // 创建Dock窗口
    QDockWidget *dockWidget = new QDockWidget("Dock Window", &mainWindow);
    dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    QLabel *dockLabel = new QLabel("Dock Window Content");
    dockWidget->setWidget(dockLabel);
    mainWindow.addDockWidget(Qt::LeftDockWidgetArea, dockWidget);

    // 创建MDI区域和子窗口
    QMdiArea *mdiArea = new QMdiArea(&mainWindow);
    mainWindow.setCentralWidget(mdiArea);

    QMdiSubWindow *subWindow1 = new QMdiSubWindow;
    subWindow1->setWidget(new QLabel("MDI SubWindow 1"));
    mdiArea->addSubWindow(subWindow1);

    QMdiSubWindow *subWindow2 = new QMdiSubWindow;
    subWindow2->setWidget(new QLabel("MDI SubWindow 2"));
    mdiArea->addSubWindow(subWindow2);

    mainWindow.show();

    return app.exec();
}

1. Create a Dock window: By creating a QDockWidget and adding it to the QMainWindow, we create a dockable child window that contains a QLabel widget as content.

2. Using Multiple Document Interface (MDI): By creating QMdiArea as a central component and creating multiple QMdiSubWindow, we implement a multi-document interface. Each QMdiSubWindow contains a QLabel as content.

running result:

Insert image description here

Summarize

This article ends here. In the next article, I will take you to continue learning QT.

Guess you like

Origin blog.csdn.net/m0_49476241/article/details/132950862