首页 > 编程语言 >QT5笔记:27. MDI应用程序设计

QT5笔记:27. MDI应用程序设计

时间:2023-03-16 13:34:17浏览次数:41  
标签:MDI triggered form Form void 27 QT5 ui MainWindow

MDI:Multiple Document Interface 多窗口文档界面

image

例子:

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMdiSubWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_actDoc_New_triggered();

    void on_actDoc_Open_triggered();

    void on_actFont_triggered();

    void on_actCopy_triggered();

    void on_actPaste_triggered();

    void on_actCut_triggered();


    void on_actViewMode_triggered(bool checked);

    void on_actCascade_triggered();

    void on_actTile_triggered();

    void on_mdiArea_subWindowActivated(QMdiSubWindow *arg1);

    void on_actCloseALL_triggered();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

MainWindow.cpp

#include "form.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actDoc_New_triggered()//新建窗口
{
    Form *form = new Form(this);
    ui->mdiArea->addSubWindow(form);
    form->show();

    ui->actFont->setEnabled(true);
    ui->actCopy->setEnabled(true);
    ui->actPaste->setEnabled(true);
    ui->actCut->setEnabled(true);
}

void MainWindow::on_actDoc_Open_triggered()//文档打开
{
    bool isNew = false;//false表示可以使用当前form,否则需要new一个窗口
    Form *form;
    if (ui->mdiArea->subWindowList().size() > 0) {
        form = (Form *)ui->mdiArea->activeSubWindow()->widget();
        isNew = form->isUsed();
    } else {
        isNew = !isNew;
    }
    QString curPath = QDir::currentPath();
    QString fileName = QFileDialog::getOpenFileName(this, "文本文件", curPath, "源文件(*.h *.cpp);;所有文件(*.*)");
    if (!fileName.isEmpty()) {
        if (isNew) {
            form = new Form(this);
            ui->mdiArea->addSubWindow(form);
        }
        form->loadFileContent(fileName);
        form->show();

        ui->actFont->setEnabled(true);
        ui->actCopy->setEnabled(true);
        ui->actPaste->setEnabled(true);
        ui->actCut->setEnabled(true);
    }
}

void MainWindow::on_actFont_triggered()//字体设置
{
    Form *form = (Form *)ui->mdiArea->activeSubWindow()->widget();
    form->setFont();
}

void MainWindow::on_actCopy_triggered()//复制
{
    Form *form = (Form *)ui->mdiArea->activeSubWindow()->widget();
    form->copyText();
}

void MainWindow::on_actPaste_triggered()//粘贴
{
    Form *form = (Form *)ui->mdiArea->activeSubWindow()->widget();
    form->pasteText();
}

void MainWindow::on_actCut_triggered()//剪切
{
    Form *form = (Form *)ui->mdiArea->activeSubWindow()->widget();
    form->cutText();
}

void MainWindow::on_actViewMode_triggered(bool checked)//设置MDI模式
{
    if (checked) {
        ui->mdiArea->setViewMode(QMdiArea::TabbedView);//在tab模式下,没有堆叠或者平铺这种模式
        ui->mdiArea->setTabsClosable(true);
        ui->actTile->setEnabled(false);
        ui->actCascade->setEnabled(false);
    } else {
        ui->mdiArea->setViewMode(QMdiArea::SubWindowView);
        ui->actTile->setEnabled(true);
        ui->actCascade->setEnabled(true);
    }
}

void MainWindow::on_actCascade_triggered()//级联展开
{
    ui->mdiArea->cascadeSubWindows();
}

void MainWindow::on_actTile_triggered()//平铺展开
{
    ui->mdiArea->tileSubWindows();
}

void MainWindow::on_mdiArea_subWindowActivated(QMdiSubWindow *arg1)//状态栏显示当前文档URL
{
    if (ui->mdiArea->subWindowList().size() == 0) {
        ui->actFont->setEnabled(false);
        ui->actCopy->setEnabled(false);
        ui->actPaste->setEnabled(false);
        ui->actCut->setEnabled(false);
    } else {
        Form *form = (Form *)arg1->widget();
        ui->statusbar->showMessage(form->curFile());
    }
}

void MainWindow::on_actCloseALL_triggered()//关闭所有
{
    QList<QMdiSubWindow *> listSubWin = ui->mdiArea->subWindowList();
    QMutableListIterator<QMdiSubWindow *> iterator(listSubWin);
    while (iterator.hasNext()) {
        QMdiSubWindow *window = iterator.next();
        window->close();
    }
}

Form.h

#ifndef FORM_H
#define FORM_H

#include <QWidget>

namespace Ui {
class Form;
}

class Form : public QWidget
{
    Q_OBJECT

public:
    explicit Form(QWidget *parent = nullptr);
    ~Form();


    bool isUsed();

    void loadFileContent(QString fileName);

    QString curFile();

    void setFont();

    void copyText();

    void pasteText();

    void cutText();
private:
    Ui::Form *ui;
    bool isUsing = false;
    QString curFileName;
};

#endif // FORM_H

Form.cpp

#include "form.h"
#include "ui_form.h"

#include <QFile>
#include <QFileInfo>
#include <QFontDialog>
#include <QTextStream>

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
}

Form::~Form()
{
    delete ui;
}

bool Form::isUsed()
{
    return isUsing;
}

void Form::loadFileContent(QString fileName)//加载文本
{
    QFile file(fileName);
    if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream stream(&file);
        QString content = "";
        while (!stream.atEnd()) {
            content += stream.readLine() + "\n";

        }
        ui->plainTextEdit->clear();
        ui->plainTextEdit->setPlainText(content);
        file.close();

        curFileName = fileName;
        QFileInfo info(fileName);
        this->setWindowTitle(info.fileName());
        isUsing = true;
    }
}

QString Form::curFile()
{
    return curFileName;
}

void Form::setFont()
{
    QFont font = ui->plainTextEdit->font();
    bool ok = false;
    font = QFontDialog::getFont(&ok, font);
    ui->plainTextEdit->setFont(font);
}

void Form::copyText()
{
    ui->plainTextEdit->copy();
}

void Form::pasteText()
{
    ui->plainTextEdit->paste();
}

void Form::cutText()
{
    ui->plainTextEdit->cut();
}

界面
image

标签:MDI,triggered,form,Form,void,27,QT5,ui,MainWindow
From: https://www.cnblogs.com/echo-lovely/p/17222217.html

相关文章