程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> QT-利用C++仿制windown自帶的記事本程序V1.0,qt-windown

QT-利用C++仿制windown自帶的記事本程序V1.0,qt-windown

編輯:C++入門知識

QT-利用C++仿制windown自帶的記事本程序V1.0,qt-windown


  下班無事, 發現QT還是很好用的, 就仿制windows的記事本做了一個,未徹底DEBUG, 先拿來分享下.

windows記事本大概是這樣的:

 

大概分為以下幾步:

1. 界面用QT代碼寫,即可, QT的布局很強大

  1 //create all actions
  2 void Notepad::createActions()
  3 {
  4     newAct = new QAction(tr("新建(&N)"), this);
  5     openAct = new QAction(tr("打開(&O)"), this);
  6     saveAct = new QAction(tr("保存(&S)"), this);
  7     saveAsAct = new QAction(tr("另存為(&A)..."), this);
  8     pageSetupAct = new QAction(tr("頁面設置(&U)..."), this);
  9     printAct = new QAction(tr("打印(&P)..."), this);
 10     quitAct = new QAction(tr("退出(&Q)"), this);
 11 
 12     undoAct = new QAction(tr("撤消(&U)"), this);
 13     redoAct = new QAction(tr("重做(&R)"), this);
 14     cutAct = new QAction(tr("剪切(&T)"), this);
 15     copyAct = new QAction(tr("復制(&C)"), this);
 16     pasteAct = new QAction(tr("粘貼(&P)"), this);
 17     deleteAct = new QAction(tr("刪除(&L)"), this);
 18     findAct = new QAction(tr("查找(&F)..."), this);
 19     findNextAct = new QAction(tr("查找下一個(&N)"), this);
 20     replaceAct = new QAction(tr("替換(&E)..."), this);
 21     gotoAct = new QAction(tr("轉到(&G)..."), this);
 22     selectAllAct = new QAction(tr("全選(&A)"), this);
 23     timeAct = new QAction(tr("時間/日期(&D)"), this);
 24 
 25     autoNewLineAct = new QAction(tr("自動換行(&L)"), this);
 26     fontAct = new QAction(tr("字體(&F)..."), this);
 27 
 28     statusBarAct = new QAction(tr("狀態欄(&S)"), this);
 29 
 30     viewHelpAct = new QAction(tr("查看幫助(&H)"), this);
 31     aboutNotepadAct = new QAction(tr("關於記事本(&A)"), this);
 32 }
 33 
 34 //create all munus
 35 void Notepad::createMenus()
 36 {
 37     fileMenu = this->menuBar()->addMenu(tr("文件(&F)"));
 38     editMenu = this->menuBar()->addMenu(tr("編輯(&E)"));
 39     formatMenu = this->menuBar()->addMenu(tr("格式(&O)"));
 40     viewMenu = this->menuBar()->addMenu(tr("查看(&V)"));
 41     helpMenu = this->menuBar()->addMenu(tr("幫助(&H)"));
 42 }
 43 
 44 //create textEdit
 45 void Notepad::createOther()
 46 {
 47     clipboard = QApplication::clipboard();
 48     QWidget *widget = new QWidget;
 49     textEdit = new QTextEdit();
 50     textEdit->setAcceptRichText(false);
 51     //set font
 52     QFont font("Arial", 12);
 53     textEdit->setFont(font);
 54     updateScrollBar();
 55     updateTextEdit();
 56     QHBoxLayout *mainLayout = new QHBoxLayout;
 57     mainLayout->setMargin(0);//important
 58     mainLayout->addWidget(textEdit);
 59 
 60     widget->setLayout(mainLayout);
 61     setCentralWidget(widget);
 62 }
 63 
 64 //create all connectors
 65 void Notepad::createConnectors()
 66 {
 67     connect(textEdit, SIGNAL(undoAvailable(bool)), this, SLOT(updateUndoAct(bool)));
 68     connect(textEdit, SIGNAL(redoAvailable(bool)), this, SLOT(updateRedoAct(bool)));
 69     connect(textEdit, SIGNAL(copyAvailable(bool)), this, SLOT(updateCutAct(bool)));
 70     connect(textEdit, SIGNAL(copyAvailable(bool)), this, SLOT(updateCopyAct(bool)));
 71     connect(textEdit, SIGNAL(copyAvailable(bool)), this, SLOT(updateDeleteAct(bool)));
 72     connect(textEdit, SIGNAL(textChanged()), this, SLOT(updateFindGroupActs()));
 73     connect(textEdit, SIGNAL(textChanged()), this, SLOT(updateSelectAllAct()));
 74     connect(textEdit, SIGNAL(textChanged()), this, SLOT(updateRowCol()));
 75     connect(clipboard, SIGNAL(dataChanged()), this, SLOT(updatePasteAct()));
 76     connect(this, SIGNAL(saveSignal(bool)), this, SLOT(updateSaveAct(bool)));
 77     //new file
 78     connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
 79     //open file
 80     connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));
 81     //save file
 82     connect(saveAct, SIGNAL(triggered()), this, SLOT(saveFile()));
 83     //save as file
 84     connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAsFile()));
 85     //page setup
 86     connect(pageSetupAct, SIGNAL(triggered()), this, SLOT(pageSetup()));
 87     //print
 88     connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
 89     //close
 90     connect(quitAct, SIGNAL(triggered()), this, SLOT(quit()));
 91 
 92     //undo
 93     connect(undoAct, SIGNAL(triggered()), this, SLOT(undo()));
 94     //redo
 95     connect(redoAct, SIGNAL(triggered()), this, SLOT(redo()));
 96     //cut
 97     connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
 98     //copy
 99     connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
100     //paste
101     connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
102     //del
103     connect(deleteAct,SIGNAL(triggered()), this, SLOT(del()) );
104     //find dialog
105     connect(findAct, SIGNAL(triggered()), this, SLOT(findDialog()));
106     //find next
107     connect(findNextAct, SIGNAL(triggered()), this, SLOT(findNext()));
108     //replace dialog
109     connect(replaceAct, SIGNAL(triggered()), this, SLOT(replaceDialog()));
110     //go to dialog
111     connect(gotoAct, SIGNAL(triggered()), this, SLOT(gotoLine()));
112     //select all
113     connect(selectAllAct, SIGNAL(triggered()), this, SLOT(selectAll()));
114     //add date
115     connect(timeAct, SIGNAL(triggered()), this, SLOT(addTime()));
116     //auto new line
117     connect(autoNewLineAct, SIGNAL(triggered()), this, SLOT(autoNewLine()));
118     //font dlg
119     connect(fontAct, SIGNAL(triggered()), this, SLOT(fontDlg()));
120     //status bar
121     connect(statusBarAct, SIGNAL(triggered()), this, SLOT(myStatusBar()));
122     //view help
123     connect(viewHelpAct, SIGNAL(triggered()), this, SLOT(viewHelp()));
124     //about
125     connect(aboutNotepadAct, SIGNAL(triggered()), this, SLOT(about()));
126 }
127 
128 //add all shortcuts
129 void Notepad::joinShortcuts()
130 {
131     newAct->setShortcut(QKeySequence::New);
132     openAct->setShortcut(QKeySequence::Open);
133     saveAct->setShortcut(QKeySequence::Save);
134     //saveAsAct->setShortcut(QKeySequence::SaveAs);
135     saveAsAct->setShortcut(QKeySequence(tr("Ctrl+Shift+S")));
136     pageSetupAct->setShortcut(QKeySequence(tr("Ctrl+E")));
137     printAct->setShortcut(QKeySequence::Print);
138     quitAct->setShortcut(QKeySequence(tr("Ctrl+Q")));
139     //quitAct->setShortcut(QKeySequence::Quit);
140 
141     undoAct->setShortcut(QKeySequence::Undo);
142     redoAct->setShortcut(QKeySequence::Redo);
143     cutAct->setShortcut(QKeySequence::Cut);
144     copyAct->setShortcut(QKeySequence::Copy);
145     pasteAct->setShortcut(QKeySequence::Paste);
146     deleteAct->setShortcut(QKeySequence::Delete);
147     newAct->setShortcut(QKeySequence::New);
148 
149     findAct->setShortcut(QKeySequence::Find);
150     findNextAct->setShortcut(QKeySequence::FindNext);
151     replaceAct->setShortcut(QKeySequence::Replace);
152 
153     gotoAct->setShortcut(QKeySequence(tr("Ctrl+G")));
154     selectAllAct->setShortcut(QKeySequence::SelectAll);
155     timeAct->setShortcut(QKeySequence(tr("F5")));
156 
157     viewHelpAct->setShortcut(QKeySequence::HelpContents);
158 }
159 
160 //add all actions to menus
161 void Notepad::joinActions()
162 {
163     fileMenu->addAction(newAct);
164     fileMenu->addAction(openAct);
165     fileMenu->addAction(saveAct);
166     fileMenu->addAction(saveAsAct);
167     fileMenu->addSeparator();
168     fileMenu->addAction(pageSetupAct);
169     fileMenu->addAction(printAct);
170     fileMenu->addSeparator();
171     fileMenu->addAction(quitAct);
172 
173     editMenu->addAction(undoAct);
174     editMenu->addAction(redoAct);
175     editMenu->addSeparator();
176     editMenu->addAction(cutAct);
177     editMenu->addAction(copyAct);
178     editMenu->addAction(pasteAct);
179     editMenu->addAction(deleteAct);
180     editMenu->addSeparator();
181     editMenu->addAction(findAct);
182     editMenu->addAction(findNextAct);
183     editMenu->addAction(replaceAct);
184     editMenu->addAction(gotoAct);
185     editMenu->addSeparator();
186     editMenu->addAction(selectAllAct);
187     editMenu->addAction(timeAct);
188 
189     formatMenu->addAction(autoNewLineAct);
190     formatMenu->addAction(fontAct);
191 
192     viewMenu->addAction(statusBarAct);
193 
194     helpMenu->addAction(viewHelpAct);
195     helpMenu->addSeparator();
196     helpMenu->addAction(aboutNotepadAct);
197 }

 

2. 每個菜單的邏輯, 這個要設計以下, 不能一上來就寫代碼, 容易返工,最後亂成一團

 

 

3. 編寫代碼, 一項一項來, 就像機械3D建模一樣, 按組件來建立

這個有點多了, 就列下頭文件, 後面整理了代碼,再傳上來

 

  1 /*************************************
  2 **fileName: notepad.h
  3 **author: kakasi ([email protected])
  4 **version: v1.0
  5 **date: 2015-10-14
  6 **last edit date: 2015-10-24 by kakasi
  7 **************************************/
  8 #ifndef NOTEPAD_H
  9 #define NOTEPAD_H
 10 #include <QMainWindow>
 11 #include "common.h"
 12 
 13 class QTextEdit;
 14 class QAction;
 15 class QMenu;
 16 class QClipboard;
 17 class QPrinter;
 18 class FindDialog;
 19 class ReplaceDialog;
 20 class GotoDialog;
 21 class QLabel;
 22 class Notepad : public QMainWindow
 23 {
 24     Q_OBJECT
 25 public:
 26     Notepad();
 27 
 28 signals:
 29     void saveSignal(bool b);
 30 
 31 private slots:
 32     //========update acts=======
 33     void updateUndoAct(bool b);
 34     void updateRedoAct(bool b);
 35     void updateCutAct(bool b);
 36     void updateCopyAct(bool b);
 37     void updateDeleteAct(bool b);
 38     void updatePasteAct();
 39     void updateSaveAct(bool b);
 40     void updateFindGroupActs();
 41     void updateSelectAllAct();
 42     void remberOldPosition();
 43     void printPreview(QPrinter *printer);
 44     //========actions=========
 45     void newFile();
 46     void openFile();
 47     bool saveFile();
 48     bool saveAsFile();
 49     void pageSetup();
 50     void print();
 51     void quit();
 52 
 53     void undo();
 54     void redo();
 55     void cut();
 56     void copy();
 57     void paste();
 58     void del();
 59 
 60     void findDialog();
 61     //find string
 62     void findString(FindData *findData);
 63     //find next
 64     void findNext();
 65 
 66     //replace dialog
 67     void replaceDialog();
 68     //replace
 69     void replace();
 70     //replace all
 71     void replaceAll();
 72     //goto
 73     void gotoLine();
 74     //go to sub
 75     void gotoLineSub(int lineNumber);
 76     //select all
 77     void selectAll();
 78     //date
 79     void addTime();
 80     //auto new line
 81     void autoNewLine();
 82     //font dialog
 83     void fontDlg();
 84     //status bar
 85     void myStatusBar();
 86 
 87     //update row and col
 88     void updateRowCol();
 89     //view help
 90     void viewHelp();
 91     //about
 92     void about();
 93 private:
 94     //create all actions
 95     void createActions();
 96     //create all menus
 97     void createMenus();
 98     //create textEdit
 99     void createOther();
100     //create all connectors
101     void createConnectors();
102     //add all shortcuts
103     void joinShortcuts();
104     //add all actions to menus
105     void joinActions();
106 
107     //init size
108     void initSize();
109     //initialize menu status
110     void initMenuStatus();
111     //init status bar
112     void initStatusBar();
113     //init variables
114     void initVariables();
115     //init printer
116     void initPrinter();
117     //update ui
118     void updateAppUI();
119 
120     //get file name info from file path
121     void showFileNameInfo(QString filePath);
122     //set file title and suffix
123     void setFileInfo(QString filePath);
124 
125     //new file sub
126     bool newFileSub();
127     //open file sub
128     bool openFileSub();
129     //get file path
130     QString getOpenedFilePath();
131     //tip to save
132     int isToSave();
133     //save file
134     bool saveFileSub(QString filePath);
135     //get file path
136     QString getSavedFilePath(QString fileTitle = "");
137     //get file type from dialog selected type
138     QString getSelectedType(QString selectedType);
139     //replace sub
140     void replaceSub();
141     //update go to act
142     void updategotoAct();
143     //update scroll bar
144     void updateScrollBar();
145     //update text edit
146     void updateTextEdit();
147     //update status bar
148     void updateStatusBarAct();
149     //get column
150     int getColumn();
151     //get line
152     int getLine();
153     //update status bar
154     void updateStatusBar();
155 private:
156     //others
157     bool statusChecked;
158     bool autoChecked;
159     int oldPosition;
160     QStatusBar *m_statusBar;
161     QLabel *statusLabel;
162     GotoDialog *gotoDlg;
163     FindDialog *findDlg;
164     ReplaceDialog *replaceDlg;
165     FindData *findData;
166     QClipboard *clipboard;
167     QTextEdit *textEdit;
168     QPrinter *printer;
169     bool saved;
170     bool isFind;
171     QString fileTitle;
172     QString appTitle;
173     QString fileSuffix;
174     QString filePath;
175     QString fileType;
176     //all menus
177     QMenu *fileMenu;
178     QMenu *editMenu;
179     QMenu *formatMenu;
180     QMenu *viewMenu;
181     QMenu *helpMenu;
182     //all actions
183     QAction *newAct;
184     QAction *openAct;
185     QAction *saveAct;
186     QAction *saveAsAct;
187     QAction *pageSetupAct;
188     QAction *printAct;
189     QAction *quitAct;
190 
191     QAction *undoAct;
192     QAction *redoAct;
193     QAction *cutAct;
194     QAction *copyAct;
195     QAction *pasteAct;
196     QAction *deleteAct;
197     QAction *findAct;
198     QAction *findNextAct;
199     QAction *replaceAct;
200     QAction *gotoAct;
201     QAction *selectAllAct;
202     QAction *timeAct;
203 
204     QAction *autoNewLineAct;
205     QAction *fontAct;
206 
207     QAction *statusBarAct;
208 
209     QAction *viewHelpAct;
210     QAction *aboutNotepadAct;
211 };
212 
213 #endif // NOTEPAD_H

 

4. 最後的成品大概是這樣的, 其中有部分內容根據自己的喜好做了小改動, 差了一個圖標, 有時間再弄下.

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved