Detailed explanation of the use of QT QMdiArea control

        This article introduces the various operations of the QMdiArea control in detail, such as: creating a new interface , source code , adding an interface , removing a child window , setting an active child window , cascading child windows, tiled child windows, and closing the current child window. , close the current sub-window, return to the current sub-window, return to the current sub-window, return to the sub-window list , signal slot, click signal, other articles and other operations.

        In actual development, an interface may contain more than a dozen controls, and manually adjusting their positions is time-consuming and laborious. QMdiArea (Multiple Document Interface Area) provides an area that can display multiple document windows at the same time.

        There are currently thirty-five articles in this series of QT comprehensive and detailed explanations. This series of articles describes the basic operation and use of QT controls in more detail. Thank you for your attention, likes, and collections.

 This article is original by the author. Please attach the source of the article and the link to this article when reprinting.

QT QMdiArea control usage detailed directory

1 Create a new interface

2 source code

3 Add interface

4 Remove a child window

5 Set up active sub-window

6 sub-window cascade arrangement

7 sub-windows arranged in tiles

8 Close the current sub-window

9 Close all child windows

10 Return to the current subwindow

11 Return to the current subwindow

12 Return to subwindow list

13 signal slot

14 click signal

15 other articles


1 Create a new interface

2 source code

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include <QTextEdit>
#include <QMdiSubWindow>

//加入GBK申明,否则中文乱码 全局使用GBK文件操作因为有中文所以使用
#pragma execution_character_set("utf-8")
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
    void subWindowAct(QMdiSubWindow* subWindow);

private:
    Ui::MainWindow *ui;

    QString Title;
    QString Version;
    QString BlogText;

    QMdiSubWindow* subWindow1;
    QMdiSubWindow* subWindow2;
    QMdiSubWindow* subWindow3;

};
#endif // MAINWINDOW_H

3 Add interface

    subWindow1 = new QMdiSubWindow;
    subWindow2 = new QMdiSubWindow;
    subWindow3 = new QMdiSubWindow;

    subWindow1->setWidget(new QTextEdit(QString("This is window 1")));
    subWindow2->setWidget(new QTextEdit(QString("This is window 2")));
    subWindow3->setWidget(new QTextEdit(QString("This is window 3")));


    ui->mdiArea->addSubWindow(subWindow1);
    ui->mdiArea->addSubWindow(subWindow2);
    ui->mdiArea->addSubWindow(subWindow3);

4 Remove a child window

    //从QMdiArea中移除一个子窗口,但不删除它。
    ui->mdiArea->removeSubWindow(subWindow1);

5 Set up active sub-window

    //设置当前活动的子窗口。
    ui->mdiArea->setActiveSubWindow(subWindow2);

6 sub-window cascade arrangement

    //将子窗口以级联的方式排列。
    ui->mdiArea->cascadeSubWindows();

7 sub-windows arranged in tiles

    //将子窗口以平铺的方式排列。
    ui->mdiArea->tileSubWindows();

8 Close the current sub-window

    //关闭当前活动的子窗口。
    ui->mdiArea->closeActiveSubWindow();

9 Close all child windows

    //关闭所有的子窗口。
    //ui->mdiArea->closeAllSubWindows();

10 Return to the current subwindow

    //返回当前活动的子窗口
    ui->mdiArea->activeSubWindow(); //QMdiSubWindow*

11 Return to the current subwindow

    //返回当前的子窗口
    ui->mdiArea->currentSubWindow();//QMdiSubWindow*

12 Return to subwindow list

    //返回子窗口的列表
    ui->mdiArea->subWindowList();   //QList<QMdiSubWindow *>

13 signal slot

QObject::connect(ui->mdiArea, &QMdiArea::subWindowActivated, this, &MainWindow::subWindowAct);

void MainWindow::subWindowAct(QMdiSubWindow* subWindow)
{
    if (subWindow) {
        // 获取活动的子窗口的指针subWindow,并进行相应的操作
        // 例如,可以获取子窗口的标题、内容等信息
        QString windowTitle = subWindow->windowTitle(); qDebug() << "子窗口标题:" << windowTitle;
        QWidget *windowContent = subWindow->widget();   qDebug() << "子窗口内容:" << windowTitle;
    } else {
        // 当没有活动的子窗口时的处理逻辑
    }
}

14 click signal

//void mousePressEvent(QMouseEvent *event) override
//{
//    if (event->button() == Qt::LeftButton) {
//        QMdiSubWindow *subWindow = subWindowAt(event->pos());
//        if (subWindow) {
//            // 处理单击窗口事件
//        }
//    }
//    QMdiArea::mousePressEvent(event);
//}

15 other articles

QT TextEdit control_Gemini Breakpoint's Blog-CSDN Blog_qt textedit

Detailed explanation of the use of QT QComboBox_Gemini Breakpoint's Blog-CSDN Blog

Detailed explanation of QT QtableView operation_Gemini Breakpoint's Blog-CSDN Blog_qtableview addition, deletion, modification and query

Qt QStandardItemModel (1. Super detailed usage)_Gemini Breakpoint's Blog-CSDN Blog_qstandardmodel

Qt QStandardItemModel (2. Super detailed function)_Gemini Breakpoint's Blog-CSDN Blog_qstandarditemmodel click event

Detailed explanation of the use of QT QRadioButton_Gemini Breakpoint's Blog-CSDN Blog_qt radiobutton

Detailed explanation of the use of QT QLineEdit_Gemini Breakpoint's Blog-CSDN Blog_qt qlineedit

Detailed explanation of the use of Qt QMessageBox_Gemini Breakpoint's Blog-CSDN Blog_qt message

QChart line chart, pie chart, bar chart, curve chart_Gemini Breakpoint's Blog-CSDN Blog_qchart Style

Detailed explanation of QChart attributes_Gemini Breakpoint's Blog-CSDN Blog_setanimationoptions

QCharts QValueAxis use_Gemini Breakpoint's Blog-CSDN Blog_qvalueaxis

Qt 5 Waiting Prompt Box (Open Source Dynamic Graphic)_Gemini Breakpoint's Blog-CSDN Blog_qt Waiting Dialog Box

QtDataVisualization Data 3D Visualization_Gemini Breakpoint's Blog-CSDN Blog_qtdatavisualizatio

QT QSpinBox Integer Counter Control Detailed Explanation_Gemini Breakpoint's Blog-CSDN Blog
QT QDoubleSpinBox Floating Point Counter Control (Usage Detailed)_Gemini Breakpoint's Blog-CSDN Blog_qdoublespinbox Signal Slot
QT QSlider, QHorizontalSlider, QVerticalSlider Control Detailed Explanation_Gemini Breakpoint blog-CSDN blog_qslider setting step size

Detailed explanation of the use of QT QTabWidget control_Gemini Breakpoint's Blog-CSDN Blog

Detailed explanation of the use of QT QCalendarWidget control_Gemini Breakpoint's Blog-CSDN Blog

Detailed explanation of the use of QT QStackedWidget control_Gemini Breakpoint's Blog-CSDN Blog

QT QVBoxLayout vertical layout control_Gemini Breakpoint's Blog-CSDN Blog

QT QHBoxLayout horizontal layout control_Gemini breakpoint's blog-CSDN blogQT
QGridLayout grid layout control_Gemini breakpoint's blog-CSDN blog

QT QVerticalSpacer Spring Control_Gemini Breakpoint's Blog-CSDN Blog
QT QHorizontalSpacer Spring Control_Gemini Breakpoint's Blog-CSDN Blog
QT QLine Detailed Usage_Gemini Breakpoint's Blog-CSDN Blog
QT QFontComboBox Detailed Usage_Gemini Breakpoint's Blog- CSDN blog

Detailed explanation of the use of QT QScrollArea control_Gemini Breakpoint's Blog-CSDN Blog

Detailed explanation of the use of QT QToolBox control_Gemini Breakpoint's Blog-CSDN Blog

Detailed explanation of the use of QT QFrame control_Gemini Breakpoint's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/qq_37529913/article/details/132706110