下班无事, 发现QT还是很好用的, 就仿制windows的记事本做了一个,未彻底DEBUG, 先拿来分享下.

windows记事本大概是这样的:

大概分为以下几步:

1. 界面用QT代码写,即可, QT的布局很强大

 //create all actions
void Notepad::createActions()
{
newAct = new QAction(tr("新建(&N)"), this);
openAct = new QAction(tr("打开(&O)"), this);
saveAct = new QAction(tr("保存(&S)"), this);
saveAsAct = new QAction(tr("另存为(&A)..."), this);
pageSetupAct = new QAction(tr("页面设置(&U)..."), this);
printAct = new QAction(tr("打印(&P)..."), this);
quitAct = new QAction(tr("退出(&Q)"), this); undoAct = new QAction(tr("撤消(&U)"), this);
redoAct = new QAction(tr("重做(&R)"), this);
cutAct = new QAction(tr("剪切(&T)"), this);
copyAct = new QAction(tr("复制(&C)"), this);
pasteAct = new QAction(tr("粘贴(&P)"), this);
deleteAct = new QAction(tr("删除(&L)"), this);
findAct = new QAction(tr("查找(&F)..."), this);
findNextAct = new QAction(tr("查找下一个(&N)"), this);
replaceAct = new QAction(tr("替换(&E)..."), this);
gotoAct = new QAction(tr("转到(&G)..."), this);
selectAllAct = new QAction(tr("全选(&A)"), this);
timeAct = new QAction(tr("时间/日期(&D)"), this); autoNewLineAct = new QAction(tr("自动换行(&L)"), this);
fontAct = new QAction(tr("字体(&F)..."), this); statusBarAct = new QAction(tr("状态栏(&S)"), this); viewHelpAct = new QAction(tr("查看帮助(&H)"), this);
aboutNotepadAct = new QAction(tr("关于记事本(&A)"), this);
} //create all munus
void Notepad::createMenus()
{
fileMenu = this->menuBar()->addMenu(tr("文件(&F)"));
editMenu = this->menuBar()->addMenu(tr("编辑(&E)"));
formatMenu = this->menuBar()->addMenu(tr("格式(&O)"));
viewMenu = this->menuBar()->addMenu(tr("查看(&V)"));
helpMenu = this->menuBar()->addMenu(tr("帮助(&H)"));
} //create textEdit
void Notepad::createOther()
{
clipboard = QApplication::clipboard();
QWidget *widget = new QWidget;
textEdit = new QTextEdit();
textEdit->setAcceptRichText(false);
//set font
QFont font("Arial", );
textEdit->setFont(font);
updateScrollBar();
updateTextEdit();
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->setMargin();//important
mainLayout->addWidget(textEdit); widget->setLayout(mainLayout);
setCentralWidget(widget);
} //create all connectors
void Notepad::createConnectors()
{
connect(textEdit, SIGNAL(undoAvailable(bool)), this, SLOT(updateUndoAct(bool)));
connect(textEdit, SIGNAL(redoAvailable(bool)), this, SLOT(updateRedoAct(bool)));
connect(textEdit, SIGNAL(copyAvailable(bool)), this, SLOT(updateCutAct(bool)));
connect(textEdit, SIGNAL(copyAvailable(bool)), this, SLOT(updateCopyAct(bool)));
connect(textEdit, SIGNAL(copyAvailable(bool)), this, SLOT(updateDeleteAct(bool)));
connect(textEdit, SIGNAL(textChanged()), this, SLOT(updateFindGroupActs()));
connect(textEdit, SIGNAL(textChanged()), this, SLOT(updateSelectAllAct()));
connect(textEdit, SIGNAL(textChanged()), this, SLOT(updateRowCol()));
connect(clipboard, SIGNAL(dataChanged()), this, SLOT(updatePasteAct()));
connect(this, SIGNAL(saveSignal(bool)), this, SLOT(updateSaveAct(bool)));
//new file
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
//open file
connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));
//save file
connect(saveAct, SIGNAL(triggered()), this, SLOT(saveFile()));
//save as file
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAsFile()));
//page setup
connect(pageSetupAct, SIGNAL(triggered()), this, SLOT(pageSetup()));
//print
connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
//close
connect(quitAct, SIGNAL(triggered()), this, SLOT(quit())); //undo
connect(undoAct, SIGNAL(triggered()), this, SLOT(undo()));
//redo
connect(redoAct, SIGNAL(triggered()), this, SLOT(redo()));
//cut
connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
//copy
connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
//paste
connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
//del
connect(deleteAct,SIGNAL(triggered()), this, SLOT(del()) );
//find dialog
connect(findAct, SIGNAL(triggered()), this, SLOT(findDialog()));
//find next
connect(findNextAct, SIGNAL(triggered()), this, SLOT(findNext()));
//replace dialog
connect(replaceAct, SIGNAL(triggered()), this, SLOT(replaceDialog()));
//go to dialog
connect(gotoAct, SIGNAL(triggered()), this, SLOT(gotoLine()));
//select all
connect(selectAllAct, SIGNAL(triggered()), this, SLOT(selectAll()));
//add date
connect(timeAct, SIGNAL(triggered()), this, SLOT(addTime()));
//auto new line
connect(autoNewLineAct, SIGNAL(triggered()), this, SLOT(autoNewLine()));
//font dlg
connect(fontAct, SIGNAL(triggered()), this, SLOT(fontDlg()));
//status bar
connect(statusBarAct, SIGNAL(triggered()), this, SLOT(myStatusBar()));
//view help
connect(viewHelpAct, SIGNAL(triggered()), this, SLOT(viewHelp()));
//about
connect(aboutNotepadAct, SIGNAL(triggered()), this, SLOT(about()));
} //add all shortcuts
void Notepad::joinShortcuts()
{
newAct->setShortcut(QKeySequence::New);
openAct->setShortcut(QKeySequence::Open);
saveAct->setShortcut(QKeySequence::Save);
//saveAsAct->setShortcut(QKeySequence::SaveAs);
saveAsAct->setShortcut(QKeySequence(tr("Ctrl+Shift+S")));
pageSetupAct->setShortcut(QKeySequence(tr("Ctrl+E")));
printAct->setShortcut(QKeySequence::Print);
quitAct->setShortcut(QKeySequence(tr("Ctrl+Q")));
//quitAct->setShortcut(QKeySequence::Quit); undoAct->setShortcut(QKeySequence::Undo);
redoAct->setShortcut(QKeySequence::Redo);
cutAct->setShortcut(QKeySequence::Cut);
copyAct->setShortcut(QKeySequence::Copy);
pasteAct->setShortcut(QKeySequence::Paste);
deleteAct->setShortcut(QKeySequence::Delete);
newAct->setShortcut(QKeySequence::New); findAct->setShortcut(QKeySequence::Find);
findNextAct->setShortcut(QKeySequence::FindNext);
replaceAct->setShortcut(QKeySequence::Replace); gotoAct->setShortcut(QKeySequence(tr("Ctrl+G")));
selectAllAct->setShortcut(QKeySequence::SelectAll);
timeAct->setShortcut(QKeySequence(tr("F5"))); viewHelpAct->setShortcut(QKeySequence::HelpContents);
} //add all actions to menus
void Notepad::joinActions()
{
fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addAction(saveAsAct);
fileMenu->addSeparator();
fileMenu->addAction(pageSetupAct);
fileMenu->addAction(printAct);
fileMenu->addSeparator();
fileMenu->addAction(quitAct); editMenu->addAction(undoAct);
editMenu->addAction(redoAct);
editMenu->addSeparator();
editMenu->addAction(cutAct);
editMenu->addAction(copyAct);
editMenu->addAction(pasteAct);
editMenu->addAction(deleteAct);
editMenu->addSeparator();
editMenu->addAction(findAct);
editMenu->addAction(findNextAct);
editMenu->addAction(replaceAct);
editMenu->addAction(gotoAct);
editMenu->addSeparator();
editMenu->addAction(selectAllAct);
editMenu->addAction(timeAct); formatMenu->addAction(autoNewLineAct);
formatMenu->addAction(fontAct); viewMenu->addAction(statusBarAct); helpMenu->addAction(viewHelpAct);
helpMenu->addSeparator();
helpMenu->addAction(aboutNotepadAct);
}

2. 每个菜单的逻辑, 这个要设计以下, 不能一上来就写代码, 容易返工,最后乱成一团

3. 编写代码, 一项一项来, 就像机械3D建模一样, 按组件来建立

这个有点多了, 就列下头文件, 后面整理了代码,再传上来

 /*************************************
**fileName: notepad.h
**author: kakasi (gongsunyongwu@163.com)
**version: v1.0
**date: 2015-10-14
**last edit date: 2015-10-24 by kakasi
**************************************/
#ifndef NOTEPAD_H
#define NOTEPAD_H
#include <QMainWindow>
#include "common.h" class QTextEdit;
class QAction;
class QMenu;
class QClipboard;
class QPrinter;
class FindDialog;
class ReplaceDialog;
class GotoDialog;
class QLabel;
class Notepad : public QMainWindow
{
Q_OBJECT
public:
Notepad(); signals:
void saveSignal(bool b); private slots:
//========update acts=======
void updateUndoAct(bool b);
void updateRedoAct(bool b);
void updateCutAct(bool b);
void updateCopyAct(bool b);
void updateDeleteAct(bool b);
void updatePasteAct();
void updateSaveAct(bool b);
void updateFindGroupActs();
void updateSelectAllAct();
void remberOldPosition();
void printPreview(QPrinter *printer);
//========actions=========
void newFile();
void openFile();
bool saveFile();
bool saveAsFile();
void pageSetup();
void print();
void quit(); void undo();
void redo();
void cut();
void copy();
void paste();
void del(); void findDialog();
//find string
void findString(FindData *findData);
//find next
void findNext(); //replace dialog
void replaceDialog();
//replace
void replace();
//replace all
void replaceAll();
//goto
void gotoLine();
//go to sub
void gotoLineSub(int lineNumber);
//select all
void selectAll();
//date
void addTime();
//auto new line
void autoNewLine();
//font dialog
void fontDlg();
//status bar
void myStatusBar(); //update row and col
void updateRowCol();
//view help
void viewHelp();
//about
void about();
private:
//create all actions
void createActions();
//create all menus
void createMenus();
//create textEdit
void createOther();
//create all connectors
void createConnectors();
//add all shortcuts
void joinShortcuts();
//add all actions to menus
void joinActions(); //init size
void initSize();
//initialize menu status
void initMenuStatus();
//init status bar
void initStatusBar();
//init variables
void initVariables();
//init printer
void initPrinter();
//update ui
void updateAppUI(); //get file name info from file path
void showFileNameInfo(QString filePath);
//set file title and suffix
void setFileInfo(QString filePath); //new file sub
bool newFileSub();
//open file sub
bool openFileSub();
//get file path
QString getOpenedFilePath();
//tip to save
int isToSave();
//save file
bool saveFileSub(QString filePath);
//get file path
QString getSavedFilePath(QString fileTitle = "");
//get file type from dialog selected type
QString getSelectedType(QString selectedType);
//replace sub
void replaceSub();
//update go to act
void updategotoAct();
//update scroll bar
void updateScrollBar();
//update text edit
void updateTextEdit();
//update status bar
void updateStatusBarAct();
//get column
int getColumn();
//get line
int getLine();
//update status bar
void updateStatusBar();
private:
//others
bool statusChecked;
bool autoChecked;
int oldPosition;
QStatusBar *m_statusBar;
QLabel *statusLabel;
GotoDialog *gotoDlg;
FindDialog *findDlg;
ReplaceDialog *replaceDlg;
FindData *findData;
QClipboard *clipboard;
QTextEdit *textEdit;
QPrinter *printer;
bool saved;
bool isFind;
QString fileTitle;
QString appTitle;
QString fileSuffix;
QString filePath;
QString fileType;
//all menus
QMenu *fileMenu;
QMenu *editMenu;
QMenu *formatMenu;
QMenu *viewMenu;
QMenu *helpMenu;
//all actions
QAction *newAct;
QAction *openAct;
QAction *saveAct;
QAction *saveAsAct;
QAction *pageSetupAct;
QAction *printAct;
QAction *quitAct; QAction *undoAct;
QAction *redoAct;
QAction *cutAct;
QAction *copyAct;
QAction *pasteAct;
QAction *deleteAct;
QAction *findAct;
QAction *findNextAct;
QAction *replaceAct;
QAction *gotoAct;
QAction *selectAllAct;
QAction *timeAct; QAction *autoNewLineAct;
QAction *fontAct; QAction *statusBarAct; QAction *viewHelpAct;
QAction *aboutNotepadAct;
}; #endif // NOTEPAD_H

4. 最后的成品大概是这样的, 其中有部分内容根据自己的喜好做了小改动, 差了一个图标, 有时间再弄下.

QT-利用C++仿制windown自带的记事本程序V1.0的更多相关文章

  1. sphider 丁廷臣简体中文完美汉化版带蜘蛛搜索引擎程序 v1.3.4

    sphider 丁廷臣简体中文完美汉化版带蜘蛛搜索引擎程序 v1.3.4是最官方的新版,免费开源,用官方最新发布原版汉化.未更改任何内核文件. Sphider 是一个完美的带有蜘蛛的搜索引擎程序. S ...

  2. [TFRecord格式数据]利用TFRecords存储与读取带标签的图片

    利用TFRecords存储与读取带标签的图片 原创文章,转载请注明出处~ 觉得有用的话,欢迎一起讨论相互学习~Follow Me TFRecords其实是一种二进制文件,虽然它不如其他格式好理解,但是 ...

  3. Qt配置cmake;运行带参数的程序

    配置cmake编译器,步骤如下: 步骤1:  Qt下新建一个project. 步骤2:  在该project目录下创建一个CMakeLists.txt文件,并按规范编写该文件. Tip: projec ...

  4. [转]利用C#自带组件强壮程序日志

    利用C#自带组件强壮程序日志   在项目正式上线后,如果出现错误,异常,崩溃等情况 我们往往第一想到的事就是查看日志 所以日志对于一个系统的维护是非常重要的 声明 正文中的代码只是一个栗子,一个非常简 ...

  5. 利用ASP.NET里自带的站点地图工具制作网站站点地图

    站点地图很方便能快速给我们导航我们要去访问的地址,能按层级关系分门别类,给用户一个很好的用户体验,很好的看到自己当前所在的网站位置 站点地图,又称网站地图,它就是一个页面,上面放置了网站上所有页面的链 ...

  6. 转载——利用C#自带组件强壮程序日志

    利用C#自带组件强壮程序日志   在项目正式上线后,如果出现错误,异常,崩溃等情况 我们往往第一想到的事就是查看日志 所以日志对于一个系统的维护是非常重要的 声明 正文中的代码只是一个栗子,一个非常简 ...

  7. 利用MAC OS X 自带的磁盘工具提取光盘镜像ISO文件

    虽说渐渐地Mac笔记本基本告别内置光驱时代了,随着网络的普及,使用到光驱的机会也渐少,但有时又难免需要光驱,比如二货出版社的随书光盘等…我们可以通过USB外置光驱将光盘内容提取为ISO文件保存到电脑里 ...

  8. qt利用QT designer构建第一个界面helloworld工程

    qt利用QT designer构建第一个界面helloworld工程原创ZJE_ANDY 发布于2017-04-07 20:25:28 阅读数 6613 收藏展开第一步:点击New Project 第 ...

  9. 利用Linux中的计划任务+PHP网页程序(转)

    利用Linux中的计划任务+PHP网页程序,实现对web服务器运行状况的监测[每5分钟监测一次,并邮件提醒]一.我的监测服务器环境:rhel5.5+apache2.2+php5二.功能描述:写一个PH ...

随机推荐

  1. JavaScript数组知识网络

    JavaScript数据类型 基本数据类型 Boolean Null Number String Symbol Undefined 对象数据类型Object Build-in object Array ...

  2. UVA 11754 Code Feat (枚举,中国剩余定理)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud C Code Feat   The government hackers at C ...

  3. jQuery中的$.extend方法总结

    原文见:jQuery.extend()函数详解 Jquery的扩展方法extend是我们在写插件的过程中常用的方法,但是经常容易搞不清楚以下两个写法的关系: 1.$.extend(dest,src1, ...

  4. jquery 单选框整个选中

    问题:遇到单选框,如图 解决办法:利用jqurey click->checked <!DOCTYPE html> <html lang="en"> & ...

  5. mongodb GUI

    官网:http://robomongo.org/ 提供Mac .Windows .Linux三种版本: robomongo界面简洁功能强大人性化:

  6. 从一道面试题谈linux下fork的运行机制

    http://www.cnblogs.com/leoo2sk/archive/2009/12/11/talk-about-fork-in-linux.html

  7. JavaScript 之 Cookie

    JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的. 而cookie是运行在客户端的,所以可以用JS来设置cookie. 假设有这样一 ...

  8. javascript之Arguments

    一.Arguments.callee //获取当前正在执行的函数,也就是这个函数自身,常用于获取匿名函数自身 语法:arguments.callee var factorial = function ...

  9. 【转】Linux里如何查找文件内容

    原文网址:http://blog.chinaunix.net/uid-25266990-id-199887.html Linux查找文件内容的常用命令方法. 从文件内容查找匹配指定字符串的行: $ g ...

  10. < IOS > 文件中 某个类设置ARC,或者非ARC

    用-fno-objc-arc标记来禁用在ARC工程那些不支持ARC的文件的ARC用-fobjc-arc标记启用非ARC工程中支持ARC的文件 项目targets -> build phases ...