diff --git a/QtMDIEx/ExMDI.cpp b/QtMDIEx/ExMDI.cpp index e84a4ce..8c01a78 100644 --- a/QtMDIEx/ExMDI.cpp +++ b/QtMDIEx/ExMDI.cpp @@ -19,8 +19,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - - #include "ExMDI.h" #include "ui_ExMDI.h" @@ -29,9 +27,65 @@ ExMDI::ExMDI(QWidget *parent) : ui(new Ui::ExMDI) { ui->setupUi(this); + + setWindowTitle("多文档界面MDI (Multi-dociment Interface) 的介绍和使用"); + setAttribute(Qt::WA_DeleteOnClose); //无论是否设置关闭时候删除;在MDI中关闭一个MDI子窗口都会删除子窗口对象 + this->setWindowIcon(QIcon("/Users/yuanyi/picture/发布、头像、测试图片/icon.png")); } ExMDI::~ExMDI() { delete ui; } + +//打开文件 +void ExMDI::loadFromFile(QString &fileName) +{ + QFile file(fileName); //文件以读的方式读出 + + if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { + QTextStream stream(&file); //以文本流方式读取文件 + ui->plainTextEdit->clear(); + ui->plainTextEdit->setPlainText(stream.readAll()); + file.close(); + + m_currentFile = fileName; + QFileInfo fileInfo(fileName); //文件信息 + setWindowTitle(fileInfo.fileName()); + m_fileOpened = true; + } +} + +QString ExMDI::currentFileName() +{ + return m_currentFile; +} + +bool ExMDI::isFileOpended() +{ + return m_fileOpened; +} + +void ExMDI::setEditFont() +{ + QFont font = ui->plainTextEdit->font(); + bool ok; + font = QFontDialog::getFont(&ok, font); + ui->plainTextEdit->setFont(font); + +} + +void ExMDI::textCut() +{ + ui->plainTextEdit->cut(); +} + +void ExMDI::textCopy() +{ + ui->plainTextEdit->copy(); +} + +void ExMDI::textPaste() +{ + ui->plainTextEdit->paste(); +} diff --git a/QtMDIEx/ExMDI.h b/QtMDIEx/ExMDI.h index 4911be7..984ddca 100644 --- a/QtMDIEx/ExMDI.h +++ b/QtMDIEx/ExMDI.h @@ -19,11 +19,16 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - #ifndef EXMDI_H #define EXMDI_H #include +#include +#include +#include +#include +#include +#include namespace Ui { class ExMDI; @@ -37,8 +42,22 @@ public: explicit ExMDI(QWidget *parent = nullptr); ~ExMDI(); + void loadFromFile(QString& fileName); //打开文件 + QString currentFileName(); //返回当前文件名 + bool isFileOpended(); //文件已经打开 + + void setEditFont(); + void textCut(); + void textCopy(); + void textPaste(); + + private: Ui::ExMDI *ui; + + QString m_currentFile; //当前文件 + bool m_fileOpened; //true 打开;false 未打开 }; #endif // EXMDI_H +; diff --git a/QtMDIEx/ExMDI.ui b/QtMDIEx/ExMDI.ui index f5ad54c..857c926 100644 --- a/QtMDIEx/ExMDI.ui +++ b/QtMDIEx/ExMDI.ui @@ -1,20 +1,25 @@ + ExMDI - - + + 0 0 - 400 - 300 + 933 + 552 - + ExMDI + + + + + - - + diff --git a/QtMDIEx/ExMainWindow.cpp b/QtMDIEx/ExMainWindow.cpp new file mode 100644 index 0000000..a75a299 --- /dev/null +++ b/QtMDIEx/ExMainWindow.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved. + * + * Author: touwoyimuli + * + * github: https://github.com/touwoyimuli + * blogs: https://touwoyimuli.github.io/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "ExMainWindow.h" +#include "ui_ExMainWindow.h" + +ExMainWindow::ExMainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::ExMainWindow) +{ + ui->setupUi(this); + setCentralWidget(ui->mdiArea); + setWindowState(Qt::WindowMaximized); + ui->toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); +} + +ExMainWindow::~ExMainWindow() +{ + delete ui; +} + +void ExMainWindow::closeEvent(QCloseEvent *e) +{ + ui->mdiArea->closeAllSubWindows(); //关闭所有子窗口 + e->accept(); +} + +void ExMainWindow::on_actOpen_triggered() +{ + bool needNew = false; + + ExMDI *mdi = nullptr; + + if (ui->mdiArea->subWindowList().count() > 0) { //如果有打开的主窗口,获取活动窗口 + mdi = (ExMDI *)ui->mdiArea->activeSubWindow()->widget(); + needNew = mdi->isFileOpended(); //文件已经打开,需要新建窗口 + } else { + needNew = true; + } + + QString curPath = QDir::currentPath(); + QString fileName = QFileDialog::getOpenFileName(this, "打开一个文件", curPath, "C程序文件(*.h *cpp);;文本文件(*.txt);;所有文件(*.*)"); + + if (fileName.isEmpty()) + return; + + if (needNew) { + mdi = new ExMDI(this); + ui->mdiArea->addSubWindow(mdi); + } + + mdi->loadFromFile(fileName); + mdi->show(); + + ui->actCopy->setEnabled(true); + ui->actCut->setEnabled(true); + ui->actPaste->setEnabled(true); + ui->actFont->setEnabled(true); +} diff --git a/QtMDIEx/ExMainWindow.h b/QtMDIEx/ExMainWindow.h new file mode 100644 index 0000000..67526f3 --- /dev/null +++ b/QtMDIEx/ExMainWindow.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved. + * + * Author: touwoyimuli + * + * github: https://github.com/touwoyimuli + * blogs: https://touwoyimuli.github.io/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef EXMAINWINDOW_H +#define EXMAINWINDOW_H + +#include +#include +#include +#include +#include + +#include "ExMDI.h" + +namespace Ui { +class ExMainWindow; +} + +class ExMainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit ExMainWindow(QWidget *parent = nullptr); + ~ExMainWindow(); + +private slots: + void on_actOpen_triggered(); + +private: + void closeEvent(QCloseEvent *e); //主窗口关闭时关闭所有子窗口 + +private: + Ui::ExMainWindow *ui; +}; + +#endif // EXMAINWINDOW_H diff --git a/QtMDIEx/ExMainWindow.ui b/QtMDIEx/ExMainWindow.ui new file mode 100644 index 0000000..636ab14 --- /dev/null +++ b/QtMDIEx/ExMainWindow.ui @@ -0,0 +1,203 @@ + + + ExMainWindow + + + + 0 + 0 + 800 + 600 + + + + MainWindow + + + + + + 100 + 70 + 561 + 321 + + + + + + + + toolBar + + + TopToolBarArea + + + false + + + + + + + + + + + + + + + + + + + + :/images/6.jpg:/images/6.jpg + + + 打开 + + + 打开文件 + + + Ctrl+O + + + + + + :/images/7.png:/images/7.png + + + 退出 + + + 退出程序 + + + Ctrl+Q + + + + + + :/images/8.png:/images/8.png + + + 新建 + + + 新建文档 + + + Ctrl+N + + + + + + :/images/10.png:/images/10.png + + + 字体 + + + 设置字体 + + + + + + :/images/11.png:/images/11.png + + + 剪切 + + + 剪切文本 + + + + + + :/images/12.png:/images/12.png + + + 拷贝 + + + 拷贝文本 + + + + + + :/images/5.png:/images/5.png + + + 粘贴 + + + 粘贴文本 + + + + + true + + + + :/images/4.png:/images/4.png + + + MDI模式 + + + 页面模式 + + + + + + :/images/2.png:/images/2.png + + + 级联展开 + + + 窗口级联展开 + + + + + + :/images/1.png:/images/1.png + + + 平铺展开 + + + 窗口平铺展开 + + + + + + :/images/13.jpg:/images/13.jpg + + + 关闭全部 + + + 关闭全部窗口 + + + + + + + + diff --git a/QtMDIEx/QtMDIEx.pro b/QtMDIEx/QtMDIEx.pro index 6383025..eb7601f 100644 --- a/QtMDIEx/QtMDIEx.pro +++ b/QtMDIEx/QtMDIEx.pro @@ -26,15 +26,21 @@ CONFIG += c++11 SOURCES += \ main.cpp \ - ExMDI.cpp + ExMDI.cpp \ + ExMainWindow.cpp HEADERS += \ - ExMDI.h + ExMDI.h \ + ExMainWindow.h FORMS += \ - ExMDI.ui + ExMDI.ui \ + ExMainWindow.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target + +RESOURCES += \ + resources.qrc diff --git a/QtMDIEx/images/.DS_Store b/QtMDIEx/images/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/QtMDIEx/images/.DS_Store differ diff --git a/QtMDIEx/images/1.png b/QtMDIEx/images/1.png new file mode 100644 index 0000000..b1f3061 Binary files /dev/null and b/QtMDIEx/images/1.png differ diff --git a/QtMDIEx/images/10.png b/QtMDIEx/images/10.png new file mode 100644 index 0000000..75bcb89 Binary files /dev/null and b/QtMDIEx/images/10.png differ diff --git a/QtMDIEx/images/11.png b/QtMDIEx/images/11.png new file mode 100644 index 0000000..62caed9 Binary files /dev/null and b/QtMDIEx/images/11.png differ diff --git a/QtMDIEx/images/12.png b/QtMDIEx/images/12.png new file mode 100644 index 0000000..4daa923 Binary files /dev/null and b/QtMDIEx/images/12.png differ diff --git a/QtMDIEx/images/13.jpg b/QtMDIEx/images/13.jpg new file mode 100755 index 0000000..a6b06e2 Binary files /dev/null and b/QtMDIEx/images/13.jpg differ diff --git a/QtMDIEx/images/2.png b/QtMDIEx/images/2.png new file mode 100644 index 0000000..9b46c34 Binary files /dev/null and b/QtMDIEx/images/2.png differ diff --git a/QtMDIEx/images/3.png b/QtMDIEx/images/3.png new file mode 100644 index 0000000..721cca7 Binary files /dev/null and b/QtMDIEx/images/3.png differ diff --git a/QtMDIEx/images/4.png b/QtMDIEx/images/4.png new file mode 100644 index 0000000..d3057fb Binary files /dev/null and b/QtMDIEx/images/4.png differ diff --git a/QtMDIEx/images/5.png b/QtMDIEx/images/5.png new file mode 100644 index 0000000..14cf1ee Binary files /dev/null and b/QtMDIEx/images/5.png differ diff --git a/QtMDIEx/images/6.jpg b/QtMDIEx/images/6.jpg new file mode 100644 index 0000000..3c54a12 Binary files /dev/null and b/QtMDIEx/images/6.jpg differ diff --git a/QtMDIEx/images/7.png b/QtMDIEx/images/7.png new file mode 100644 index 0000000..b6d915f Binary files /dev/null and b/QtMDIEx/images/7.png differ diff --git a/QtMDIEx/images/8.png b/QtMDIEx/images/8.png new file mode 100644 index 0000000..0faeea9 Binary files /dev/null and b/QtMDIEx/images/8.png differ diff --git a/QtMDIEx/images/9.png b/QtMDIEx/images/9.png new file mode 100644 index 0000000..1f342d7 Binary files /dev/null and b/QtMDIEx/images/9.png differ diff --git a/QtMDIEx/main.cpp b/QtMDIEx/main.cpp index 712af0f..3a23def 100644 --- a/QtMDIEx/main.cpp +++ b/QtMDIEx/main.cpp @@ -1,10 +1,11 @@ #include "ExMDI.h" +#include "ExMainWindow.h" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); - ExMDI w; + ExMainWindow w; w.show(); return a.exec(); diff --git a/QtMDIEx/resources.qrc b/QtMDIEx/resources.qrc new file mode 100644 index 0000000..37dfad6 --- /dev/null +++ b/QtMDIEx/resources.qrc @@ -0,0 +1,17 @@ + + + images/1.png + images/2.png + images/3.png + images/4.png + images/5.png + images/6.jpg + images/7.png + images/8.png + images/9.png + images/10.png + images/11.png + images/12.png + images/13.jpg + +