一、界面实现
二·、逻辑思路
1、创建mainwindow窗体,对窗体中中加入menu和action,并加入快捷键(&N),
2、添加资源图片文件(项目—右键—添加新文件—QT—Qt Resource File—choose—添加资源文件名—下一步),对资源文件中添加现有图片文件,双击每个action加入相应图片
3、新建文件函数:在主窗体中加入mdiArea,可多subwidget增加,设计subText类,并在主函数的action_new点击槽函数中写入创建局部subText指针,并在mdiArea中添加subText实例化对象,最后用show()函数显示子类。改变文档标题,数字逐渐递增,编辑后增加*号,对当前窗体内容改变的信号映射到槽,槽函数实现当前窗体更新
4、打开文件函数:
调用 QFileDialog::getOpenFileName(this,“获取文本文件”,".","Text(.txt .h .cpp)") 打开文件对话框
调用QFile定义的指针myfile myfile ::open(QIODevice::ReadOnly|QIODevice::ReadOnly) 打开文件
调用QMessageBox::warning(this,“打开失败”,“打开文件失败”) 显示文件打开失败警告
调用QTextStream类定义流变量,以文本流形式读取文件内容(此处要用while进行文件流读取)
1
2
3
4
5while(!stream.atEnd()){ QString str = stream.readLine(); this->append(str); }
5、保存文件
子类函数:QFileDialog(获取保存的文件名)、QFile(添加文件名,并打开文件)、QMessageBox(显示返回值得中文含义)、QTextStream的形式读取文件 saveStream<toPlainText();
主窗体:QMdiSubWindow获取活动子窗体、获取子窗体widget内容、定义子类指针并强制转换widget,最后调用子类保存文件函数
6、文件另存为
在保存文件之前清空文件名,对文件实现另存为。
三、代码
程序运行窗体如下:
mainwindow.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include "subtext.h" #include <QMdiSubWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; void init(); QString codeName; subText *getActiveChildForm(); private slots: //文件 void onprogressTriggeredByNew(bool); void onprogressTriggeredByOpen(bool); void onprogressTriggeredByUTF_8(bool); void onprogressTriggeredByGB18030(bool); void onprogressTriggeredBySave(bool); void onprogressTriggeredBySaveAs(bool); void onprogressTriggeredByExit(bool); //编辑 void onprogressTriggeredByCut(bool); void onprogressTriggeredByCopy(bool); void onprogressTriggeredByPaste(bool); void onprogressTriggeredByUndo(bool); void onprogressTriggeredByRedo(bool); //窗口 void onprogressTriggeredByClose(bool); void onprogressTriggeredByAllClose(bool); void onprogressTriggeredByTile(bool); void onprogressTriggeredByCascade(bool); void onprogressTriggeredByNext(bool); void onprogressTriggeredByPrevious(bool); //帮助 void onprogressTriggeredByAbout(bool); void onprogressTriggeredByAboutQt(bool); }; #endif // MAINWINDOW_H
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); init(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::init() { codeName = "UTF-8"; connect(ui->action_New,SIGNAL(triggered(bool)),this,SLOT(onprogressTriggeredByNew(bool))); connect(ui->action_open,SIGNAL(triggered(bool)),this,SLOT(onprogressTriggeredByOpen(bool))); connect(ui->action_UTF_8,SIGNAL(triggered(bool)),this,SLOT(onprogressTriggeredByUTF_8(bool))); connect(ui->action_GB2312,SIGNAL(triggered(bool)),this,SLOT(onprogressTriggeredByGB18030(bool))); connect(ui->action_Save,SIGNAL(triggered(bool)),this,SLOT(onprogressTriggeredBySave(bool))); connect(ui->action_asaveAs,SIGNAL(triggered(bool)),this,SLOT(onprogressTriggeredBySaveAs(boo))); connect(ui->action_Exit,SIGNAL(triggered(bool)),this,SLOT(onprogressTriggeredByExit(bool))); //窗口 connect(ui->action_close,SIGNAL(triggered(bool)),this,SLOT(onprogressTriggeredByClose(bool))); connect(ui->action_closeAll,SIGNAL(triggered(bool)),this,SLOT(onprogressTriggeredByAllClose(bool))); connect(ui->action_Tile,SIGNAL(triggered(bool)),this,SLOT(onprogressTriggeredByTile(bool))); connect(ui->action_Cascade,SIGNAL(triggered(bool)),this,SLOT(onprogressTriggeredByCascade(bool))); connect(ui->action_Next,SIGNAL(triggered(bool)),this,SLOT(onprogressTriggeredByNext(bool))); connect(ui->action_Pervies,SIGNAL(triggered(bool)),this,SLOT(onprogressTriggeredByPrevious(bool))); //帮助 connect(ui->action_About,SIGNAL(triggered(bool)),this,SLOT(onprogressTriggeredByAbout(bool))); connect(ui->action_AboutQT,SIGNAL(triggered(bool)),this,SLOT(onprogressTriggeredByAboutQt(bool))); } //----------------function----------------// //--------------------end-------------------// //---------------------slots---------------------// void MainWindow::onprogressTriggeredByNew(bool) { subText *mysubText = new subText; ui->mdiArea->addSubWindow((QWidget *)mysubText); mysubText->setCodeName(codeName); mysubText->newFile(); mysubText->show(); } void MainWindow::onprogressTriggeredByOpen(bool) { subText *mysubText = new subText; ui->mdiArea->addSubWindow((QWidget *)mysubText); mysubText->setCodeName(codeName); mysubText->openFile(); mysubText->show(); } void MainWindow::onprogressTriggeredBySave(bool) { QMdiSubWindow *m_sub = ui->mdiArea->activeSubWindow(); if(m_sub == NULL) return; QWidget *wid = m_sub->widget(); if(wid == NULL) return; subText *mysubText = (subText *)wid; if(mysubText == NULL) return; mysubText->saveFile(); mysubText->close(); } void MainWindow::onprogressTriggeredBySaveAs(bool) { QMdiSubWindow *m_sub = ui->mdiArea->activeSubWindow(); if(m_sub == NULL) return; QWidget *wid = m_sub->widget(); if(wid == NULL) return; subText *mysubText = (subText *)wid; if(mysubText == NULL) return; mysubText->saveAsFile(); } subText *MainWindow::getActiveChildForm() { subText *sub = NULL; QMdiSubWindow *subWin = ui->mdiArea->activeSubWindow(); if(subWin == NULL) return sub; QWidget *wid = subWin->widget(); if(wid == NULL) return sub; sub = (subText *)wid; return sub; } void MainWindow::onprogressTriggeredByExit(bool) { ui->mdiArea->closeAllSubWindows(); this->close(); } void MainWindow::onprogressTriggeredByUTF_8(bool) { this->codeName = "UTF-8"; } void MainWindow::onprogressTriggeredByGB18030(bool) { this->codeName = "GB18030"; } //编辑 void MainWindow::onprogressTriggeredByCut(bool) { subText *sub = getActiveChildForm(); if(sub != NULL) sub->cut(); } void MainWindow::onprogressTriggeredByCopy(bool) { subText *sub = getActiveChildForm(); if(sub != NULL) sub->copy(); } void MainWindow::onprogressTriggeredByPaste(bool) { subText *sub = getActiveChildForm(); if(sub != NULL) sub->paste(); } void MainWindow::onprogressTriggeredByUndo(bool) { subText *sub = getActiveChildForm(); if(sub != NULL) sub->undo(); } void MainWindow::onprogressTriggeredByRedo(bool) { subText *sub = getActiveChildForm(); if(sub != NULL) sub->redo(); } //窗口 void MainWindow::onprogressTriggeredByClose(bool) { ui->mdiArea->closeActiveSubWindow(); } void MainWindow::onprogressTriggeredByAllClose(bool) { ui->mdiArea->closeAllSubWindows(); } void MainWindow::onprogressTriggeredByTile(bool) { ui->mdiArea->tileSubWindows(); } void MainWindow::onprogressTriggeredByCascade(bool) { ui->mdiArea->cascadeSubWindows(); } void MainWindow::onprogressTriggeredByNext(bool) { ui->mdiArea->activateNextSubWindow(); } void MainWindow::onprogressTriggeredByPrevious(bool) { ui->mdiArea->activatePreviousSubWindow(); } //帮助 void MainWindow::onprogressTriggeredByAbout(bool) { QMessageBox::about(this,"关于作者","owener by cxy885588"); } void MainWindow::onprogressTriggeredByAboutQt(bool) { QMessageBox::aboutQt(this,"关于QT"); } //-----------------------end------------------//
subtext.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38#ifndef SUBTEXT_H #define SUBTEXT_H #include <QWidget> #include <QTextEdit> #include <QFileDialog> #include <QFile> #include <QMessageBox> #include <QTextStream> #include <QCloseEvent> #include <QContextMenuEvent> class subText : public QTextEdit { Q_OBJECT public: explicit subText(QWidget *parent = 0); void newFile(); void openFile(); int saveFile(); void saveAsFile(); void setCodeName(const QString); protected: void closeEvent(QCloseEvent *event); void contextMenuEvent(QContextMenuEvent *event); signals: private: QString fileName; QFile *myfile; QString mycodename; bool isEdit; public slots: void onprogressByNew(); }; #endif // SUBTEXT_H
subtext.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167#include "subtext.h" #include <QMenu> #include <QContextMenuEvent> subText::subText(QWidget *parent) : QTextEdit(parent) { isEdit = false;//默认情况下文件没有被修改 fileName.clear(); myfile = new QFile(this); } //-------------function---------------// void subText::newFile() { static int index = 1;//创建命名累加数 QString title = QString("未命名的文本[%1]""[*]").arg(index);//设置新建文件标题字符串 index++;//累加新建文件数目 this->setWindowTitle(title);//设置文件标题 connect(this->document(),SIGNAL(contentsChanged()),this,SLOT(onprogressByNew()));//创建文件更改后的动态槽 } void subText::openFile() { //QFileDialog获取文件路径 QString filename = QFileDialog::getOpenFileName(this,"获取文本文件", ".", "Text (*.txt *.h *.cpp)"); if(filename.isEmpty()) return; // 设置文件路径 this->fileName = filename; // 拆分文件名 QStringList list = filename.split("/"); QString title = list.at(list.length()-1); this->setWindowTitle(title); // 设置文件名 myfile->setFileName(title); bool ret = myfile->open(QIODevice::ReadOnly|QIODevice::Text); if(!ret){ QMessageBox::warning(this,"打开失败","打开文件失败"); return; } QTextStream stream(myfile); stream.setCodec(this->mycodename.toLocal8Bit().data()); while(!stream.atEnd()){ QString str = stream.readLine(); this->append(str); } myfile->close(); } int subText::saveFile() { int ret = -1; //if(!isEdit)return; if(this->fileName.isEmpty()){ QString m_filename = QFileDialog::getSaveFileName(this,"保存文件",".","Text(*.cpp *.h *.txt)"); if(m_filename.isEmpty()){ return ret; } this->fileName = m_filename; } myfile->setFileName(this->fileName); bool saveRet = myfile->open(QIODevice::WriteOnly|QIODevice::Text); if(!saveRet){ QMessageBox::warning(this,"保存失败","保存文件失败"); return ret; } QTextStream saveStream(myfile); saveStream.setCodec(this->mycodename.toLocal8Bit().data()); saveStream<<this->toPlainText(); saveStream.flush(); myfile->close(); //this->setWindowModified(false); isEdit = true; QMessageBox::information(this,"成功提示","成功保存文件"); ret = 0; return ret; } void subText::saveAsFile() { bool isedit = this->isEdit; this->isEdit = true; QString m_filename = this->fileName; this->fileName.clear(); bool ret = saveFile(); if(ret<0) { this->isEdit = isedit; this->fileName = m_filename; } } void subText::setCodeName(const QString codename) { this->mycodename = codename; } //-------------end-----------------// //--------------slots-------------------// void subText::onprogressByNew() { isEdit = true;//文件被修改 this->setWindowModified(true);//更新窗体 } void subText::closeEvent(QCloseEvent *event) { if(!isEdit){ return; } QMessageBox::StandardButton ret = QMessageBox::information(this,"提示信息", "确定是否需要保存", QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel,QMessageBox::Yes); if(ret == QMessageBox::Yes) { saveFile(); event->accept(); } else if(ret == QMessageBox::No){ event->accept(); } else { event->ignore(); } } //-------------event---------------// void subText::contextMenuEvent(QContextMenuEvent *e) { QMenu *myMenu = new QMenu(this); QAction *redo = myMenu->addAction(QIcon(":/pic/redo.png"), "撤销",this,SLOT(redo()),QKeySequence::Redo); redo->setEnabled(this->document()->isRedoAvailable()); QAction *undo = myMenu->addAction(QIcon(":/pic/undo.png"), "恢复",this,SLOT(undo()),QKeySequence::Undo); undo->setEnabled(this->document()->isRedoAvailable()); myMenu->addSeparator(); QAction *copy = myMenu->addAction(QIcon(":/pic/copy.png"), "复制",this,SLOT(copy()),QKeySequence::Copy); copy->setEnabled(this->textCursor().hasSelection()); QAction *cut = myMenu->addAction(QIcon(":/pic/cut.png"), "剪切",this,SLOT(cut()),QKeySequence::Cut); cut->setEnabled(this->textCursor().hasSelection()); QAction *paste = myMenu->addAction(QIcon(":/pic/paste.png"), "粘贴",this,SLOT(paste()),QKeySequence::Paste); myMenu->addSeparator(); QAction *clear = myMenu->addAction(QIcon(":/pic/clear.png"), "清除",this,SLOT(clear()),QKeySequence(tr("Ctrl+P"))); clear->setEnabled(!this->document()->isEmpty()); QAction *select = myMenu->addAction(QIcon(":/pic/selectAll.png"), "全选",this,SLOT(selectAll()),QKeySequence::SelectAll); select->setEnabled(!this->document()->isEmpty()); myMenu->exec(e->globalPos()); } //---------------end---------------//
最后
以上就是体贴灯泡最近收集整理的关于QT:MainWindow窗体设计(八)的全部内容,更多相关QT内容请搜索靠谱客的其他文章。
发表评论 取消回复