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
|
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMessageBox>
#include <QTextEdit>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QFont>
#include <QFontDialog>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
bool textChanged=false;
private:
void createMenu();
void setShortCut();
int popUpMsgBox();
QTextEdit* mainEdit;
QString currentFileName="";
public slots:
void slotTextChanged();
void slotNewFile();
void slotOpenFile();
void slotUndo();
void slotSaveAs();
void slotSaveFile();
void slotClose();
void slotFont();
void slotAbout();
};
#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
|
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
createMenu();
mainEdit = new QTextEdit(this);
this->setCentralWidget(mainEdit);
this->resize(900,600);
connect(mainEdit,SIGNAL(textChanged()),this,SLOT(slotTextChanged()));
}
void MainWindow::createMenu(){
const int MENU_CNT = 2;
const int MAX_ACTION=10;
const int actionsCnt[MENU_CNT]={5,3};
const int MAXSLOT = 8;
const char* actions[MAXSLOT][2] = {
{SLOT(slotNewFile()),"New File"}
,{SLOT(slotOpenFile()),"Open file"}
,{SLOT(slotSaveAs()),"Save as..."}
,{SLOT(slotSaveFile()),"Save"}
,{SLOT(slotClose()),"Close File"}
,{SLOT(slotUndo()),"Undo"}
,{SLOT(slotFont()),"Font"}
,{SLOT(slotAbout()),"About"}
};
QMenuBar *menuBar = new QMenuBar(this);
QMenu *menu[MENU_CNT];
menu[0] = new QMenu("File");
menu[1] = new QMenu("Edit");
QAction * action[MENU_CNT][MAX_ACTION];
int actionIdx=0;
for(int i=0;i<MENU_CNT;++i){
for(int j=0;j<actionsCnt[i];++j){
action[i][j] = new QAction(actions[actionIdx][1]);
menu[i]->addAction(action[i][j]);
connect(action[i][j],SIGNAL(triggered(bool)),this,actions[actionIdx++][0]);
}
menuBar->addMenu(menu[i]);
}
this->setMenuBar(menuBar);
}
void MainWindow::slotTextChanged(){
qDebug()<<"slotTextChanged";
this->textChanged = true;
}
int MainWindow::popUpMsgBox(){
QMessageBox msgBox;
msgBox.setText("The document has been modified.");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard| QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
return msgBox.exec();
}
void MainWindow::slotNewFile(){
qDebug()<<"slotNewFile";
if(textChanged){
int btn = popUpMsgBox();
if(btn==QMessageBox::Save) slotSaveFile();
else if(btn==QMessageBox::Cancel) return;
}
currentFileName = "";
this->setWindowTitle("");
mainEdit->clear();
textChanged=false;
}
void MainWindow::slotOpenFile(){
qDebug()<<"slotOpenFile";
if(textChanged){
int btn = popUpMsgBox();
if(btn==QMessageBox::Save) slotSaveFile();
}
QString fileName = QFileDialog::getOpenFileName(this,"Open");
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly|QFile::Text)) return;
this->setWindowTitle(currentFileName=fileName);
QTextStream in(&file);
mainEdit->setText(in.readAll());
file.close();
this->textChanged = false;
}
void MainWindow::slotSaveAs(){
qDebug()<<"slotSaveAs";
QString fileName = QFileDialog::getSaveFileName(this,"Save");
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly|QFile::Text))return;
currentFileName=fileName;
this->setWindowTitle(fileName);
QTextStream out(&file);
out<<mainEdit->toPlainText();
file.close();
this->textChanged = false;
}
void MainWindow::slotSaveFile(){
qDebug()<<"slotSaveFile";
qDebug()<<currentFileName;
if(currentFileName.isEmpty()){
slotSaveAs();
return;
}
qDebug()<<currentFileName;
QString fileName = currentFileName;
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly|QFile::Text))return;
QTextStream out(&file);
out<<mainEdit->toPlainText();
file.close();
this->textChanged = false;
}
void MainWindow::slotClose(){
qDebug()<<"slotClose";
if(textChanged){
int btn = popUpMsgBox();
if(btn==QMessageBox::Save) slotSaveFile();
else if(btn==QMessageBox::Cancel) return;
}
this->close();
}
void MainWindow::slotUndo(){
qDebug()<<"slotUndo";
mainEdit->undo();
}
void MainWindow::slotFont(){
bool ok;
QFont font = QFontDialog::getFont(&ok,QFont("Helvetica [Cronyx]",10),this);
if(ok) mainEdit->setFont(font);
else return;
}
void MainWindow::slotAbout(){
QMessageBox::about(this,tr("About Notepad"),tr("........."));
}
MainWindow::~MainWindow()
{
}
|
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
|
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
|
'프로그래밍 > QT' 카테고리의 다른 글
[프로그래밍/Qt] Calculator 계산기 기능 구현 예제 (0) | 2019.05.21 |
---|