feat: MDI(Multi-document Interface)
@ -19,8 +19,6 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://touwoyimuli.github.io/>.
|
||||
*/
|
||||
|
||||
|
||||
#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();
|
||||
}
|
||||
|
@ -19,11 +19,16 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://touwoyimuli.github.io/>.
|
||||
*/
|
||||
|
||||
#ifndef EXMDI_H
|
||||
#define EXMDI_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QTextStream>
|
||||
#include <QFileInfo>
|
||||
#include <QFontDialog>
|
||||
#include <QFile>
|
||||
#include <QIODevice>
|
||||
#include <QIcon>
|
||||
|
||||
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
|
||||
;
|
||||
|
@ -1,20 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>ExMDI</class>
|
||||
<widget class="QWidget" name="ExMDI" >
|
||||
<property name="geometry" >
|
||||
<widget class="QWidget" name="ExMDI">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<height>300</height>
|
||||
<width>933</width>
|
||||
<height>552</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle" >
|
||||
<property name="windowTitle">
|
||||
<string>ExMDI</string>
|
||||
</property>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QPlainTextEdit" name="plainTextEdit"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<layoutDefault spacing="6" margin="11" />
|
||||
<pixmapfunction></pixmapfunction>
|
||||
<layoutdefault spacing="6" margin="11"/>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
|
77
QtMDIEx/ExMainWindow.cpp
Normal file
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
|
||||
*
|
||||
* Author: touwoyimuli <touwoyimuli@gmai.com>
|
||||
*
|
||||
* 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 <https://touwoyimuli.github.io/>.
|
||||
*/
|
||||
#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);
|
||||
}
|
55
QtMDIEx/ExMainWindow.h
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
|
||||
*
|
||||
* Author: touwoyimuli <touwoyimuli@gmai.com>
|
||||
*
|
||||
* 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 <https://touwoyimuli.github.io/>.
|
||||
*/
|
||||
#ifndef EXMAINWINDOW_H
|
||||
#define EXMAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QCloseEvent>
|
||||
#include <QDir>
|
||||
#include <QFileDialog>
|
||||
#include <QMdiSubWindow>
|
||||
|
||||
#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
|
203
QtMDIEx/ExMainWindow.ui
Normal file
@ -0,0 +1,203 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>ExMainWindow</class>
|
||||
<widget class="QMainWindow" name="ExMainWindow">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>600</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>MainWindow</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralwidget">
|
||||
<widget class="QMdiArea" name="mdiArea">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>100</x>
|
||||
<y>70</y>
|
||||
<width>561</width>
|
||||
<height>321</height>
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusbar"/>
|
||||
<widget class="QToolBar" name="toolBar">
|
||||
<property name="windowTitle">
|
||||
<string>toolBar</string>
|
||||
</property>
|
||||
<attribute name="toolBarArea">
|
||||
<enum>TopToolBarArea</enum>
|
||||
</attribute>
|
||||
<attribute name="toolBarBreak">
|
||||
<bool>false</bool>
|
||||
</attribute>
|
||||
<addaction name="actNew"/>
|
||||
<addaction name="actOpen"/>
|
||||
<addaction name="separator"/>
|
||||
<addaction name="actFont"/>
|
||||
<addaction name="actCopy"/>
|
||||
<addaction name="actCut"/>
|
||||
<addaction name="actPaste"/>
|
||||
<addaction name="separator"/>
|
||||
<addaction name="actView"/>
|
||||
<addaction name="actCascade"/>
|
||||
<addaction name="actTile"/>
|
||||
<addaction name="actClose"/>
|
||||
<addaction name="separator"/>
|
||||
<addaction name="actQuit"/>
|
||||
</widget>
|
||||
<action name="actOpen">
|
||||
<property name="icon">
|
||||
<iconset resource="resources.qrc">
|
||||
<normaloff>:/images/6.jpg</normaloff>:/images/6.jpg</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>打开</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>打开文件</string>
|
||||
</property>
|
||||
<property name="shortcut">
|
||||
<string>Ctrl+O</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actQuit">
|
||||
<property name="icon">
|
||||
<iconset resource="resources.qrc">
|
||||
<normaloff>:/images/7.png</normaloff>:/images/7.png</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>退出</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>退出程序</string>
|
||||
</property>
|
||||
<property name="shortcut">
|
||||
<string>Ctrl+Q</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actNew">
|
||||
<property name="icon">
|
||||
<iconset resource="resources.qrc">
|
||||
<normaloff>:/images/8.png</normaloff>:/images/8.png</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>新建</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>新建文档</string>
|
||||
</property>
|
||||
<property name="shortcut">
|
||||
<string>Ctrl+N</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actFont">
|
||||
<property name="icon">
|
||||
<iconset resource="resources.qrc">
|
||||
<normaloff>:/images/10.png</normaloff>:/images/10.png</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>字体</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>设置字体</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actCut">
|
||||
<property name="icon">
|
||||
<iconset resource="resources.qrc">
|
||||
<normaloff>:/images/11.png</normaloff>:/images/11.png</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>剪切</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>剪切文本</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actCopy">
|
||||
<property name="icon">
|
||||
<iconset resource="resources.qrc">
|
||||
<normaloff>:/images/12.png</normaloff>:/images/12.png</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>拷贝</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>拷贝文本</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actPaste">
|
||||
<property name="icon">
|
||||
<iconset resource="resources.qrc">
|
||||
<normaloff>:/images/5.png</normaloff>:/images/5.png</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>粘贴</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>粘贴文本</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actView">
|
||||
<property name="checkable">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset resource="resources.qrc">
|
||||
<normaloff>:/images/4.png</normaloff>:/images/4.png</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>MDI模式</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>页面模式</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actCascade">
|
||||
<property name="icon">
|
||||
<iconset resource="resources.qrc">
|
||||
<normaloff>:/images/2.png</normaloff>:/images/2.png</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>级联展开</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>窗口级联展开</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actTile">
|
||||
<property name="icon">
|
||||
<iconset resource="resources.qrc">
|
||||
<normaloff>:/images/1.png</normaloff>:/images/1.png</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>平铺展开</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>窗口平铺展开</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actClose">
|
||||
<property name="icon">
|
||||
<iconset resource="resources.qrc">
|
||||
<normaloff>:/images/13.jpg</normaloff>:/images/13.jpg</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>关闭全部</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>关闭全部窗口</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<resources>
|
||||
<include location="resources.qrc"/>
|
||||
</resources>
|
||||
<connections/>
|
||||
</ui>
|
@ -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
|
||||
|
BIN
QtMDIEx/images/.DS_Store
vendored
Normal file
BIN
QtMDIEx/images/1.png
Normal file
After Width: | Height: | Size: 280 B |
BIN
QtMDIEx/images/10.png
Normal file
After Width: | Height: | Size: 1.5 KiB |
BIN
QtMDIEx/images/11.png
Normal file
After Width: | Height: | Size: 1018 B |
BIN
QtMDIEx/images/12.png
Normal file
After Width: | Height: | Size: 659 B |
BIN
QtMDIEx/images/13.jpg
Executable file
After Width: | Height: | Size: 592 B |
BIN
QtMDIEx/images/2.png
Normal file
After Width: | Height: | Size: 318 B |
BIN
QtMDIEx/images/3.png
Normal file
After Width: | Height: | Size: 421 B |
BIN
QtMDIEx/images/4.png
Normal file
After Width: | Height: | Size: 454 B |
BIN
QtMDIEx/images/5.png
Normal file
After Width: | Height: | Size: 472 B |
BIN
QtMDIEx/images/6.jpg
Normal file
After Width: | Height: | Size: 1.8 KiB |
BIN
QtMDIEx/images/7.png
Normal file
After Width: | Height: | Size: 1.6 KiB |
BIN
QtMDIEx/images/8.png
Normal file
After Width: | Height: | Size: 1.5 KiB |
BIN
QtMDIEx/images/9.png
Normal file
After Width: | Height: | Size: 1.7 KiB |
@ -1,10 +1,11 @@
|
||||
#include "ExMDI.h"
|
||||
#include "ExMainWindow.h"
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
ExMDI w;
|
||||
ExMainWindow w;
|
||||
w.show();
|
||||
|
||||
return a.exec();
|
||||
|
17
QtMDIEx/resources.qrc
Normal file
@ -0,0 +1,17 @@
|
||||
<RCC>
|
||||
<qresource prefix="/">
|
||||
<file>images/1.png</file>
|
||||
<file>images/2.png</file>
|
||||
<file>images/3.png</file>
|
||||
<file>images/4.png</file>
|
||||
<file>images/5.png</file>
|
||||
<file>images/6.jpg</file>
|
||||
<file>images/7.png</file>
|
||||
<file>images/8.png</file>
|
||||
<file>images/9.png</file>
|
||||
<file>images/10.png</file>
|
||||
<file>images/11.png</file>
|
||||
<file>images/12.png</file>
|
||||
<file>images/13.jpg</file>
|
||||
</qresource>
|
||||
</RCC>
|