qt5.5实现 记事本程序

最近由于要做Qt相关的毕业设计课题,以前对Qt完全不了解,对于客户端图形界面程序,也只对Windows下的MFC熟悉,

所以,由于Qt的跨平台特性和相对比较纯的C++的特点,就准备学习一下吧。这两天逛了一下Qt的开发者官方网站,和一些国内的论坛,

看了下基本的知识点,比如信号和槽的机制,界面的布局,就想写个东西,巩固一下。于是参考了官方的文档和代码,纯代码实现了一个简易的记事本程序。

希望大家参考学习,共同进步。不足之处,还请指出。

记事本简介:记事本采用代码实现GUI界面,支持,新建,保存,另存为,查找,替换,打印功能。

开发环境:win7旗舰版 32位 Qt Creator   Qt 5.5

参考资料:http://www.devbean.net/2012/08/qt-study-road-2-intro

http://doc.qt.io/qt-5/gettingstartedqt.html

 

 

界面外观:

qt5.5实现 记事本程序_第1张图片

qt5.5实现 记事本程序_第2张图片

查找功能:

qt5.5实现 记事本程序_第3张图片

替换功能:

qt5.5实现 记事本程序_第4张图片

打印功能:

qt5.5实现 记事本程序_第5张图片

保存等:

qt5.5实现 记事本程序_第6张图片

 

关于本屌狮

qt5.5实现 记事本程序_第7张图片

 

本程序涉及一个主对话框,一个查找对话框,一个替换对话框,一个打印对话框,一个关于信息对话框,一个打开对话框,一个保存对话框,其他信息提示对话框。

功能设计:主要涉及对话框的布局,主对话框与查找,替换对话框的交互。当然这是通过信号与槽的机制来实现的。下面看下具体的代码实现。

主对话框类

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include <QMenu>
 6 #include <QMenuBar>
 7 #include <QAction>
 8 #include <QTextEdit>
 9 #include <QIcon>
10 #include <QFile>
11 #include <QFileDialog>
12 #include <QTextStream>
13 #include <QMessageBox>
14 #include <QTextDocument>
15 #include <QTextCursor>
16 #include <QToolBar>
17 #include <QtPrintSupport/QPrinter>
18 #include <QtPrintSupport/QPrintDialog>
19 #include <QtPrintSupport/QPrintPreviewDialog>
20 #include <QtPrintSupport/QPageSetupDialog>
21 
22 #include <finddialog.h>
23 
24 #include <replacedialog.h>
25 
26 class MainWindow : public QMainWindow
27 {
28     Q_OBJECT
29 public:
30     MainWindow(QWidget *parent = 0);
31     ~MainWindow();
32 protected:
33 private slots:
34     void createMenus();
35     void createActions();
36     void newAct();
37     void openAct();
38     void saveAct();
39     void printAct();
40     void exitAct();
41     void findAct(QString,bool,bool);
42     void aboutAct();
43     void anotherSaveAct();
44     void showFindDialog();
45     void showReplaceDialog();
46     void findReplace(QString,bool);
47     void replaceCur(QString,QString);
48     void replaceAll(QString,QString,bool);
49 
50 private:
51     //菜单
52     bool   find;
53     QMenu *file_menu;
54     QMenu *edit_menu;
55     QMenu *about_menu;
56     //Action响应file_menu
57     QAction *new_act;
58     QAction *open_act;
59     QAction *save_act;
60     QAction *another_save_act;
61     QAction *print_act;
62     QAction *exit_act;
63     //Action响应edit_menu
64     QAction *find_act;
65     QAction *replace_act;
66     //Action响应help_menu
67     QAction *about_act;
68     //Edit
69     QTextEdit *mini_text;
70 
71     FindDialog *find_dialog;
72     ReplaceDialog *replace_dialog;
73     QString cur_file_name;
74 
75 };
76 
77 #endif // MAINWINDOW_H
  1 #include "mainwindow.h"
  2 MainWindow::MainWindow(QWidget *parent)
  3     : QMainWindow(parent)
  4 {
  5     setWindowTitle(tr("MiniText"));
  6     mini_text = new QTextEdit;
  7     this->setCentralWidget(mini_text);
  8     resize(900,500);
  9     cur_file_name = tr("");
 10 
 11     createActions();
 12     createMenus();
 13 }
 14 
 15 MainWindow::~MainWindow()
 16 {
 17    if(file_menu){
 18        delete file_menu;
 19    }
 20    if(edit_menu){
 21        delete edit_menu;
 22    }
 23    if(about_menu){
 24        delete about_menu;
 25    }
 26    if(new_act){
 27        delete new_act;
 28    }
 29    if(open_act){
 30        delete open_act;
 31    }
 32    if(save_act){
 33        delete save_act;
 34    }
 35    if(print_act){
 36        delete print_act;
 37    }
 38    if(exit_act){
 39        delete exit_act;
 40 
 41    }
 42    if(find_act){
 43        delete find_act;
 44 
 45    }
 46    if(replace_act){
 47        delete replace_act;
 48    }
 49    if(about_act){
 50        delete about_act;
 51    }
 52    if(mini_text){
 53        delete mini_text;
 54    }
 55    if(find_dialog){
 56        delete find_dialog;
 57    }
 58    if(replace_dialog){
 59        delete replace_dialog;
 60    }
 61    if(another_save_act){
 62        delete another_save_act;
 63    }
 64 }
 65 
 66 void MainWindow::createMenus()
 67 {
 68 
 69     file_menu = menuBar()->addMenu(tr("&文件"));
 70     file_menu->setStyleSheet("color:blue");
 71     file_menu->addAction(new_act);
 72     file_menu->addAction(open_act);
 73     file_menu->addAction(save_act);
 74     file_menu->addAction(another_save_act);
 75     file_menu->addAction(print_act);
 76     file_menu->addSeparator();
 77     file_menu->addAction(exit_act);
 78     edit_menu = menuBar()->addMenu(tr("&编辑"));
 79     edit_menu->setStyleSheet("color:blue");
 80     edit_menu->addAction(find_act);
 81     edit_menu->addAction(replace_act);
 82     about_menu = menuBar()->addMenu(tr("&关于"));
 83     about_menu->setStyleSheet("color:blue");
 84     about_menu->addAction(about_act);
 85     QToolBar *tool_bar = addToolBar(tr("file"));
 86     tool_bar->addAction(new_act);
 87     tool_bar->addAction(open_act);
 88     tool_bar->addAction(save_act);
 89     tool_bar->addAction(another_save_act);
 90     tool_bar->addAction(print_act);
 91     tool_bar->addAction(find_act);
 92     tool_bar->addAction(replace_act);
 93     tool_bar->addAction(about_act);
 94     tool_bar->addAction(exit_act);
 95 
 96 }
 97 
 98 void MainWindow::createActions()
 99 {
100     find = false;
101     find_dialog = new FindDialog(this);
102     replace_dialog = new ReplaceDialog(this);
103     //Action响应file_menu
104     new_act = new QAction(QIcon(tr(":/images/new")),tr("&新建"),this);
105     new_act->setShortcut(QKeySequence::New);
106 
107     open_act = new QAction(QIcon(tr(":/images/open")),tr("&打开"),this);
108     open_act->setShortcut(QKeySequence::Open);
109 
110     save_act = new QAction(QIcon(tr(":/images/save")),tr("&保存"),this);
111     save_act->setShortcut(QKeySequence::Save);
112 
113     another_save_act = new QAction(QIcon(tr(":/images/another_save")),tr("另存为"),this);
114 
115     print_act = new QAction(QIcon(tr(":/images/print")),tr("&打印"),this);
116     print_act->setShortcut(QKeySequence::Print);
117 
118     exit_act = new QAction(QIcon(tr(":/images/exit")),tr("&退出"),this);
119     exit_act->setShortcut(QKeySequence::Quit);
120 
121     find_act = new QAction(QIcon(tr(":/images/find")),tr("&查找"),this);
122     find_act->setShortcut(QKeySequence::Find);
123 
124     replace_act = new QAction(QIcon(tr(":/images/replace")),tr("&替换"),this);
125     replace_act->setShortcut(QKeySequence::Replace);
126 
127     about_act = new QAction(QIcon(tr(":/images/about")),tr("关于"),this);
128 
129     connect(new_act,SIGNAL(triggered()),this,SLOT(newAct()));
130     connect(open_act,SIGNAL(triggered()),this,SLOT(openAct()));
131     connect(save_act,SIGNAL(triggered()),this,SLOT(saveAct()));
132     connect(print_act,SIGNAL(triggered()),this,SLOT(printAct()));
133     connect(another_save_act,SIGNAL(triggered()),this,SLOT(anotherSaveAct()));
134     connect(exit_act,SIGNAL(triggered()),this,SLOT(exitAct()));
135     connect(find_act,SIGNAL(triggered()),this,SLOT(showFindDialog()));
136     connect(replace_act,SIGNAL(triggered()),this,SLOT(showReplaceDialog()));
137     connect(about_act,SIGNAL(triggered()),this,SLOT(aboutAct()));
138 
139     connect(find_dialog,SIGNAL(findTextDataButtonClickedSignal(QString,bool,bool)),
140             this,SLOT(findAct(QString,bool,bool)));
141     connect(replace_dialog,SIGNAL(findReplaceStr(QString,bool)),
142             this,SLOT(findReplace(QString,bool)));
143     connect(replace_dialog,SIGNAL(replaceCurOneStr(QString,QString)),
144             this,SLOT(replaceCur(QString,QString)));
145     connect(replace_dialog,SIGNAL(replaceAllStr(QString,QString,bool)),
146             this,SLOT(replaceAll(QString,QString,bool)));
147 }
148 
149 void MainWindow::newAct()
150 {
151     if (mini_text->document()->isModified())
152     {
153         QMessageBox::StandardButton button = QMessageBox::information(this,
154                                           "尚未保存", "是否要保存?",QMessageBox::Save |
155                                            QMessageBox::Discard | QMessageBox::Cancel);
156         switch(button)
157         {
158             case QMessageBox::Save:{
159                 saveAct();
160                 if (mini_text->document()->isModified()){
161                     return;
162                 }
163             }
164             case QMessageBox::Cancel:{
165                 return;
166             }
167             case QMessageBox::Discard:{
168                 break;
169             }
170         }
171     }
172     mini_text->clear();
173 }
174 
175 //打开文件
176 void MainWindow::openAct()
177 {
178     QString file_name = QFileDialog::getOpenFileName(this,tr("打开文件"),QString(),
179                           tr("文本文件(*.txt) ;; C++文件(*.h *.cpp *.hpp)"));
180     cur_file_name = file_name;
181     if(!file_name.isEmpty()){
182        QFile file(file_name);
183        if(!file.open(QIODevice::ReadOnly)){
184           QMessageBox::critical(this,tr("错误"),tr("不能打开文件"));
185           return;
186        }
187        QTextStream ins(&file);
188        mini_text->setText(ins.readAll());
189        file.close();
190     }
191 }
192 
193 //保存文件
194 void MainWindow::saveAct()
195 {
196     if(cur_file_name.isEmpty()){
197         QString file_name = QFileDialog::getSaveFileName(this,tr("保存文件"),QString(),
198                                tr("文本文件(*.txt) ;; C++文件(*.h *.cpp *.hpp)"));
199         if(!file_name.isEmpty()){
200             QFile file(file_name);
201             if(!file.open(QIODevice::WriteOnly)){
202                 QMessageBox::critical(this,tr("错误"),tr("不能打开文件"));
203                 return;
204             }
205             QTextStream outs(&file);
206             outs<<mini_text->toPlainText();
207             outs.flush();
208             file.close();
209         }
210     }
211     else{
212         QFile file(cur_file_name);
213         if(!file.open(QIODevice::WriteOnly)){
214             QMessageBox::critical(this,tr("错误"),tr("不能打开文件"));
215             return;
216         }
217         QTextStream outs(&file);
218         outs<<mini_text->toPlainText();
219         outs.flush();
220         file.close();
221     }
222 }
223 
224 void MainWindow::anotherSaveAct()
225 {
226     QString file_name = QFileDialog::getSaveFileName(this,tr("保存文件"),QString(),
227                            tr("文本文件(*.txt) ;; C++文件(*.h *.cpp *.hpp)"));
228     if(!file_name.isEmpty()){
229         QFile file(file_name);
230         if(!file.open(QIODevice::WriteOnly)){
231             QMessageBox::critical(this,tr("错误"),tr("不能打开文件"));
232             return;
233         }
234         QTextStream outs(&file);
235         outs<<mini_text->toPlainText();
236         outs.flush();
237         file.close();
238     }
239 
240 }
241 
242 void MainWindow::findReplace(QString find_str, bool flg)
243 {
244     bool find_flag;
245     if(flg){
246         find_flag = mini_text->find(find_str,QTextDocument::FindCaseSensitively);
247     }
248     else{
249         find_flag = mini_text->find(find_str);
250     }
251     if(!find_flag){
252         QMessageBox::information(this,tr("结果"),tr("没有找到查找内容"),QMessageBox::Yes);
253     }
254     else{
255         find = true;
256     }
257 
258 
259 }
260 
261 void MainWindow::replaceCur(QString find_str, QString replace_str)
262 {
263     if(find){
264         QTextCursor text_cursor = mini_text->textCursor();
265         text_cursor.insertText(replace_str);
266     }
267     else{
268         QMessageBox::information(this,tr("结果"),tr("没有内容不能替换")+find_str,QMessageBox::Yes);
269     }
270     find = false;
271 }
272 
273 void MainWindow::replaceAll(QString find_str, QString replace_str,bool flg)
274 {
275     if(!flg){
276         bool haf = mini_text->find(find_str);
277         if(haf){
278             QTextCursor text_cursor = mini_text->textCursor();
279             text_cursor.insertText(replace_str);
280             while(mini_text->find(find_str)){
281                 text_cursor = mini_text->textCursor();
282                 text_cursor.insertText(replace_str);
283             }
284             while(mini_text->find(find_str,QTextDocument::FindBackward)){
285                 text_cursor = mini_text->textCursor();
286                 text_cursor.insertText(replace_str);
287             }
288         }
289         else{
290              QMessageBox::information(this,tr("结果"),tr("没有内容不能替换 ")+find_str,QMessageBox::Yes);
291         }
292     }
293     else{
294         bool haf = mini_text->find(find_str,QTextDocument::FindCaseSensitively);
295         if(haf){
296             QTextCursor text_cursor = mini_text->textCursor();
297             text_cursor.insertText(replace_str);
298             while(mini_text->find(find_str,QTextDocument::FindCaseSensitively)){
299                 text_cursor = mini_text->textCursor();
300                 text_cursor.insertText(replace_str);
301             }
302             while(mini_text->find(find_str,QTextDocument::FindCaseSensitively|QTextDocument::FindBackward)){
303                 text_cursor = mini_text->textCursor();
304                 text_cursor.insertText(replace_str);
305             }
306         }
307         else{
308             QMessageBox::information(this,tr("结果"),tr("没有内容不能替换")+find_str,QMessageBox::Yes);
309         }
310     }
311 
312 }
313 
314 void MainWindow::printAct()
315 {
316     QPrinter printer;
317     QString printer_name = printer.printerName();
318        if( printer_name.size() == 0){
319            return;
320        }
321        QPrintDialog dlg(&printer, this);
322        if (mini_text->textCursor().hasSelection()){
323            dlg.addEnabledOption(QAbstractPrintDialog::PrintSelection);
324        }
325        if(dlg.exec() == QDialog::Accepted){
326            mini_text->print(&printer);
327        }
328 }
329 
330 void MainWindow::findAct(QString str,bool ignore,bool choose)
331 {
332     QString text = str;
333     bool find_flag;
334     if(!text.isEmpty()){
335         if(choose){
336             if(ignore){
337               find_flag = mini_text->find(text,QTextDocument::FindCaseSensitively);
338             }
339             else{
340                 find_flag = mini_text->find(text);
341 
342             }
343         }
344         else{
345             if(ignore){
346                 find_flag = mini_text->find(text,QTextDocument::FindBackward|QTextDocument::FindCaseSensitively);
347             }
348             else{
349                 find_flag = mini_text->find(text,QTextDocument::FindBackward);
350 
351             }
352         }
353         if(!find_flag){
354             QMessageBox::information(this,tr("结果"),tr("没有找到查找内容"),QMessageBox::Yes);
355         }
356     }
357 
358 }
359 
360 void MainWindow::aboutAct()
361 {
362     QMessageBox message(QMessageBox::NoIcon,tr("关于"), tr("version: v1.0\n"
363                                                           "author: karllen\n"
364                                                           "qq: 1160113606\n"
365                                                          "Begin Learning The Qt"));
366     message.setIconPixmap(QPixmap(tr(":/images/me")));
367     message.exec();
368 
369 }
370 
371 void MainWindow::showFindDialog()
372 {
373     find_dialog->show();
374 }
375 
376 void MainWindow::showReplaceDialog()
377 {
378     replace_dialog->show();
379 }
380 
381 void MainWindow::exitAct()
382 {
383     this->close();
384 
385 }

查找对话框类:

 1 #ifndef FINDDIALOG_H
 2 #define FINDDIALOG_H
 3 #include <QDialog>
 4 #include <QLineEdit>
 5 #include <QLabel>
 6 #include <QPushButton>
 7 #include <QRadioButton>
 8 #include <QGridLayout>
 9 #include <QHBoxLayout>
10 #include <QGroupBox>
11 #include <QCheckBox>
12 
13 class FindDialog:public QDialog
14 {
15     Q_OBJECT
16 public:
17     FindDialog(QWidget *parent = 0);
18     ~FindDialog();
19 
20 signals:
21     void findTextDataButtonClickedSignal(QString,bool,bool);
22 
23 private slots:
24     void findButtonState();
25     void findDataButtonClickedState();
26 
27 private:
28     QLineEdit *find_edit;
29     QLabel    *find_label,*ignore_label,*next_label,*back_label;
30     QPushButton *find_button;
31     QRadioButton *next_radio;
32     QRadioButton *back_radio;
33     QCheckBox *ignore_flag;
34 };
35 
36 #endif // FINDDIALOG_H
  1 #include "finddialog.h"
  2 
  3 FindDialog::FindDialog(QWidget *parent)
  4     :QDialog(parent)
  5 {
  6     setWindowTitle(tr("查找"));
  7     find_label = new QLabel(tr("查找"));
  8     ignore_label = new QLabel(tr("区分大小写"));
  9     next_label = new QLabel(tr("向后"));
 10     back_label = new QLabel(tr("向前"));
 11     find_edit = new QLineEdit;
 12     find_button = new QPushButton(tr("查找下一个"));
 13     next_radio = new QRadioButton;
 14     back_radio = new QRadioButton;
 15     ignore_flag = new QCheckBox;
 16 
 17     find_edit->setText(tr(""));
 18 
 19     QGridLayout *grid_layout = new QGridLayout(this);
 20     grid_layout->addWidget(find_label,0,0);
 21     grid_layout->addWidget(find_edit,0,1);
 22     grid_layout->addWidget(find_button,0,3);
 23 
 24     QHBoxLayout *ignore_layout = new QHBoxLayout;
 25     ignore_layout->setSpacing(10);
 26     ignore_layout->addWidget(ignore_label);
 27     ignore_layout->addWidget(ignore_flag);
 28 
 29     QHBoxLayout *radio_layout = new QHBoxLayout;
 30     radio_layout->addWidget(next_label);
 31     radio_layout->addWidget(next_radio);
 32 
 33     radio_layout->addWidget(back_label);
 34     radio_layout->addWidget(back_radio);
 35 
 36     QGroupBox *group_radio = new QGroupBox(tr("方向"),this);
 37     group_radio->setLayout(radio_layout);
 38 
 39     QHBoxLayout *do_radio = new QHBoxLayout;
 40     do_radio->addWidget(group_radio);
 41 
 42     grid_layout->addLayout(ignore_layout,1,0);
 43     grid_layout->addLayout(do_radio,1,1);
 44 
 45     this->setMaximumSize(300,100);
 46     next_radio->setChecked(true);
 47     find_button->setEnabled(false);
 48     connect(find_edit,SIGNAL(textChanged(QString)),this,SLOT(findButtonState()));
 49     connect(find_button,SIGNAL(clicked(bool)),this,SLOT(findDataButtonClickedState()));
 50 }
 51 
 52 FindDialog::~FindDialog()
 53 {
 54     if(find_edit){
 55         delete find_edit;
 56     }
 57     if(find_label){
 58         delete find_label;
 59     }
 60     if(ignore_label){
 61         delete ignore_label;
 62     }
 63     if(next_label){
 64         delete next_label;
 65     }
 66     if(back_label){
 67         delete back_label;
 68     }
 69     if(find_button){
 70         delete find_button;
 71     }
 72     if(next_radio){
 73         delete next_radio;
 74     }
 75     if(back_radio){
 76         delete back_radio;
 77     }
 78     if(ignore_flag){
 79         delete ignore_flag;
 80     }
 81 }
 82 
 83 void FindDialog::findButtonState()
 84 {
 85     if(find_edit->text().isEmpty()){
 86         find_button->setEnabled(false);
 87     }
 88     else{
 89         find_button->setEnabled(true);
 90     }
 91 
 92 }
 93 
 94 void FindDialog::findDataButtonClickedState()
 95 {
 96     if(find_edit->text().isEmpty()){
 97         return;
 98     }
 99     QString str = find_edit->text();
100     if(next_radio->isChecked()){
101         if(ignore_flag->isChecked()){
102             emit findTextDataButtonClickedSignal(str,true,true);
103         }
104         else{
105             emit findTextDataButtonClickedSignal(str,false,true);
106         }
107     }
108     if(back_radio->isChecked()){
109         if(ignore_flag->isChecked()){
110             emit findTextDataButtonClickedSignal(str,true,false);
111         }
112         else{
113             emit findTextDataButtonClickedSignal(str,false,false);
114         }
115     }
116 }

替换对话框类:

 1 #ifndef REPLACEDIALOG_H
 2 #define REPLACEDIALOG_H
 3 
 4 #include <QDialog>
 5 #include <QPushButton>
 6 #include <QLabel>
 7 #include <QCheckBox>
 8 #include <QLineEdit>
 9 #include <QGridLayout>
10 #include <QVBoxLayout>
11 
12 class ReplaceDialog:public QDialog
13 {
14     Q_OBJECT
15 public:
16     ReplaceDialog(QWidget *parent = 0);
17     ~ReplaceDialog();
18 signals:
19     void replaceAllStr(QString , QString,bool);
20     void replaceCurOneStr(QString,QString);
21     void findReplaceStr(QString,bool);
22 private slots:
23     void chooseStrSlot();
24     void findButtonStateSlot();
25     void replaceOneButtonStateSlot();
26     void replaceAllButtonStateSlot();
27 private:
28     QLineEdit *find_str;
29     QLineEdit *replace_str;
30     QPushButton *find_next_button;
31     QPushButton *replaced_one_button;
32     QPushButton *replaced_all_button;
33     QPushButton *cancle_button;
34     QCheckBox *ignore_flag;
35     QLabel  *find_label;
36     QLabel  *replace_label;
37 };
38 
39 #endif // REPLACEDIALOG_H
  1 #include "replacedialog.h"
  2 
  3 ReplaceDialog::ReplaceDialog(QWidget *parent)
  4     :QDialog(parent)
  5 {
  6     find_label          = new QLabel(tr("查找内容"));
  7     replace_label       = new QLabel(tr("替换为"));
  8     find_str            = new QLineEdit();
  9     replace_str         = new QLineEdit();
 10     find_next_button    = new QPushButton(tr("查找下一个"));
 11     replaced_one_button = new QPushButton(tr("替换"));
 12     replaced_all_button = new QPushButton(tr("全部替换"));
 13     cancle_button       = new QPushButton(tr("取消"));
 14     ignore_flag         = new QCheckBox(tr("区分大小写"));
 15 
 16     QGridLayout *grdly  = new QGridLayout(this);
 17     QVBoxLayout *vboxly = new QVBoxLayout(this);
 18 
 19     vboxly->addWidget(replaced_all_button);
 20     vboxly->addWidget(cancle_button);
 21     grdly->addWidget(find_label,0,0);
 22     grdly->addWidget(find_str,0,1);
 23     grdly->addWidget(find_next_button,0,2);
 24     grdly->addWidget(replace_label,1,0);
 25     grdly->addWidget(replace_str,1,1);
 26     grdly->addWidget(replaced_one_button,1,2);
 27     grdly->addWidget(ignore_flag,2,1);
 28     grdly->addLayout(vboxly,2,2);
 29 
 30     find_str->setText(tr(""));
 31     replace_str->setText(tr(""));
 32     find_next_button->setEnabled(false);
 33     replaced_one_button->setEnabled(false);
 34     replaced_all_button->setEnabled(false);
 35 
 36     this->setMaximumSize(300,100);
 37 
 38     connect(find_str,SIGNAL(textChanged(QString)),this,SLOT(chooseStrSlot()));
 39     connect(cancle_button,SIGNAL(clicked()),this,SLOT(close()));
 40     connect(find_next_button,SIGNAL(clicked()),this,SLOT(findButtonStateSlot()));
 41     connect(replaced_one_button,SIGNAL(clicked()),this,SLOT(replaceOneButtonStateSlot()));
 42     connect(replaced_all_button,SIGNAL(clicked()),this,SLOT(replaceAllButtonStateSlot()));
 43 }
 44 
 45 ReplaceDialog::~ReplaceDialog()
 46 {
 47     if(find_str){
 48         delete find_str;
 49     }
 50     if(replace_str){
 51         delete replace_str;
 52     }
 53     if(find_next_button){
 54         delete find_next_button;
 55     }
 56     if(replaced_one_button){
 57         delete replaced_one_button;
 58     }
 59     if(replaced_all_button){
 60         delete replaced_all_button;
 61     }
 62     if(cancle_button){
 63         delete cancle_button;
 64     }
 65     if(ignore_flag){
 66         delete ignore_flag;
 67     }
 68     if(find_label){
 69         delete find_label;
 70     }
 71     if(replace_label){
 72         delete replace_label;
 73     }
 74 }
 75 
 76 void ReplaceDialog::chooseStrSlot()
 77 {
 78     if(!find_str->text().isEmpty()){
 79         find_next_button->setEnabled(true);
 80         replaced_one_button->setEnabled(true);
 81         replaced_all_button->setEnabled(true);
 82     }
 83     else{
 84         find_next_button->setEnabled(false);
 85         replaced_one_button->setEnabled(false);
 86         replaced_all_button->setEnabled(false);
 87     }
 88 }
 89 
 90 void ReplaceDialog::findButtonStateSlot()
 91 {
 92     if(ignore_flag->isChecked()){
 93         emit findReplaceStr(find_str->text(),true);
 94     }
 95     else{
 96         emit findReplaceStr(find_str->text(),false);
 97     }
 98 }
 99 
100 void ReplaceDialog::replaceOneButtonStateSlot()
101 {
102     emit replaceCurOneStr(find_str->text(),replace_str->text());
103 
104 }
105 
106 void ReplaceDialog::replaceAllButtonStateSlot()
107 {
108     if(ignore_flag->isChecked()){
109         emit replaceAllStr(find_str->text() , replace_str->text(),true);
110     }
111     else{
112         emit replaceAllStr(find_str->text() , replace_str->text(),false);
113     }
114 
115 
116 }

main函数:

 1 #include "mainwindow.h"
 2 #include "finddialog.h"
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8 
 9     MainWindow w;
10     w.show();
11     return a.exec();
12 }

 

注:代码下载地址 http://download.csdn.net/detail/u010085340/9289609                                       

你可能感兴趣的:(qt5.5实现 记事本程序)