《MyWord》完整源代码(原创) C++ Qt
2016-01-04 21:14
597 查看
MyWord.pro
childwnd.h
global.h
mainwindow.h
childwnd.cpp
mainwindow.cpp
mainwindow.ui
原创作品 请勿随意转载! 作者:邱于涵。QQ:1031893464
#------------------------------------------------- # # Project created by QtCreator 2015-12-06T13:53:21 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = MyWord TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp \ childwnd.cpp \ global.cpp HEADERS += mainwindow.h \ childwnd.h \ global.h FORMS += mainwindow.ui RESOURCES += \ resource.qrc
childwnd.h
#ifndef CHILDWND_H #define CHILDWND_H #include<QTextEdit> #include<QWidget> #include<QObject> #include<QTextCharFormat> class ChildWnd:public QTextEdit { /***********使用信号槽 必须加 Q_OBJECT******************/ /**************如果设置了Q_OBJECT后 连接器报错 需要把生成的 目录删除了 重新编译即可***********************/ Q_OBJECT public: ChildWnd(QWidget *parent=0); ~ChildWnd(); //|设置当前文件 URL bool mSetCurrentFileName(QString fileurl); //|载入文件 bool mLoadFile(QString filepath); public: //|属性 /*isModified字面上就是 “是 更改了” 具体上就是 一个标示符 *如果为true 那么 文档内的内容有变化 马上更改 标题栏 加*然后设置成 false *文件保存了 更改变标题栏 *去掉 然后设置成 true * */ bool isModified;//|是否更改了内容 //|当前文件的URL QString 指针 QString * pCurrentFileUrl; //|设置当前字体样式 void mMergeFormatOnWordOrSelection(QTextCharFormat format); public slots: //|文档改动 void slotDocumentWasModiied(); }; #endif // CHILDWND_H
global.h
#ifndef GLOBAL_H #define GLOBAL_H /************* 全局变量声明 ******/ extern int numberChild; #endif // GLOBAL_H
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include<QString> #include <QMainWindow> #include<QMdiArea> #include<QMdiSubWindow> class ChildWnd; namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); //|创建Mdi bool mCreateMdi(); //|创建菜单 bool mCreateMenu(); //|创建工具栏 bool mCreateToolBar(); //|创建子窗口 bool mCreateChildWnd(QString WindowTitle=""); //|返回活动窗口指针(子窗口) ChildWnd * mActiveMyChild(); public: //|Mdi 指针 QMdiArea * pMdiArea; //|文件菜单 项Action指针 QAction *pActionNewFile; QAction * pActionOpenFile; QAction * pActionSaveFile; QAction * pActionSaveOtherFile; //|编辑菜单项 Action指针 //|格式菜单项 Action指针 QAction * pActionTextBold;//加粗 QAction * pActionTextItalic;//倾斜 QAction * pActionTextUnderLine;//下划线 QAction * pActionColor;//设置颜色 QAction * pActionBlockRight; /*段落对齐 左中右 两端**/ QAction * pActionBlockLeft; QAction * pActionBlockCenter; QAction * pActionBlockBoth; //|窗口菜单项 Action指针 //|帮助菜单项Action指针 private slots: //|更新菜单 根据 Mdi的 SubWindowActive(QMdiSubWindow*)信号 然后判断 是否有活动窗口 void UpdateMenu(QMdiSubWindow*); //|新建 文件菜单项 triggered()信号槽 void slotFileNewFileAction(); //|保存文件 菜单项 信号槽 void slotFileSaveAction(); //|字体加粗 void slotTextBold(); //|打开文件 void slotActionOpen(); //|设置Font Family void slotActionFontFamily(QString family); //|设置Font Size void slotsetFontSize(QString size); //|设置字体Italic 倾斜 void slotsetFontItalic(); //|设置字体颜色 void slotsetTextColor(); //|设置字体下划线 void slotSetTextUnderLine(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
childwnd.cpp
#include "childwnd.h" #include<QTextDocument> #include<QUrl> #include<QTextCursor> #include<QFile> #include<QByteArray> ChildWnd::ChildWnd(QWidget * parent):QTextEdit(parent) { //|子窗口在关闭时 销毁这个对象 setAttribute(Qt::WA_DeleteOnClose); //|当前文件的Url this->pCurrentFileUrl=new QString; //|更改了 this->isModified=true; //|文档改动过后 更改子窗口 标题 connect(this->document(),SIGNAL(contentsChanged()),this,SLOT(slotDocumentWasModiied())); } ChildWnd::~ChildWnd() { } //|文档改动槽 void ChildWnd::slotDocumentWasModiied() { //this->setWindowTitle("未命名*"); if(this->isModified==true) { //|更改了 就加* this->setWindowTitle(this->windowTitle()+QString("*")); //|标识还原回去 this->isModified=false; } } //|设置当前文件URL bool ChildWnd::mSetCurrentFileName(QString fileurl) { //|赋值属性 *(this->pCurrentFileUrl)=fileurl; //|赋值标题栏 先构造QUrl 返回出 文件名称 QUrl url(fileurl); //|返回文件名称 QString filename= url.fileName(); //|设置标题栏 this->setWindowTitle(filename); return true; } //|设置当前字体样式 void ChildWnd::mMergeFormatOnWordOrSelection(QTextCharFormat format) { //|获取textCursor QTextCursor cursor=this->textCursor(); /*如果没有选择字体 * if(cursor.hasSelection()==false) { cursor.select(QTextCursor::WordUnderCursor); } */ /*Merge 就是合并的意思 ,怎么回事?把传入的参数的字体样式 混合到当前的 字体的样式上 * 比如 “你好” 这两个字的大小 是 12 颜色是蓝色 但是merge了一个 大小为6的字体format对象 * 不会重新设置颜色 只会重新设置样式 因为 是merge混合的 * */ cursor.mergeCharFormat(format); // this->mergeCurrentCharFormat(format); } //|载入文件 bool ChildWnd::mLoadFile(QString filepath) { QFile file(filepath); file.open(QIODevice::ReadOnly); if(file.exists()==false) { return false; }else{ QByteArray data=file.readAll(); this->setHtml(data); this->mSetCurrentFileName(filepath); } return true; }global.cpp
//|全局变量实现 #include"global.h" //|窗体编号 int numberChild=1;main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include"childwnd.h" #include<qmenu.h> #include<QAction> #include<QMessageBox> #include<QPushButton> #include<QComboBox> #include<QTextEdit> #include<QMdiSubWindow> #include<QFileDialog> #include<QTextDocument> #include<QTextDocumentWriter> #include<QTextCharFormat> #include<qfont.h> #include"childwnd.h" #include<QFontComboBox> #include<QFontDatabase> #include<QList> #include<QKeySequence> #include<QIcon> #include"global.h" #include<QColorDialog> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //|创建mdi this->mCreateMdi(); //|创建菜单 this->mCreateMenu(); //|创建工具栏 this->mCreateToolBar(); // ChildWnd * c=new ChildWnd; //this->pMdiArea->addSubWindow(c); } MainWindow::~MainWindow() { delete ui; } //|创建mdi bool MainWindow::mCreateMdi() { this->pMdiArea=new QMdiArea; //|设置中央控件 this->setCentralWidget(this->pMdiArea); /*更新菜单项 根据Mdi的 子窗口 活动 信号 * 注意:这个Mdi的subWidnowActive这个窗口活动信号 在子窗口(新建或者关闭)都会触发 * 判断有没有活动窗口还需要 调用QMdiArea :: activeSubWindow() 看返回值 * 调用ChildWnd对象 需要QMdiArea :: activeSubWindow() ::widget(); * */ connect(this->pMdiArea,SIGNAL(subWindowActivated(QMdiSubWindow*)),this,SLOT(UpdateMenu(QMdiSubWindow*))); return true; } //|创建子窗口 bool MainWindow::mCreateChildWnd(QString WindowTitle) { //|实例化一个子窗口类 ChildWnd * pWnd=new ChildWnd; pWnd->setWindowTitle(WindowTitle); //|加入Mdi this->pMdiArea->addSubWindow(pWnd); //|(重点) 这里一定要show一次 pWnd->show(); return true; } //|创建菜单 bool MainWindow::mCreateMenu() { /*设计器上面有MenuBar了 setStatusTip是为一个菜单项设置状态栏提示(当鼠标移上去时) QMenu::addSeparator是在菜单项列表中添加一条分割线 tr()宏 里面 用&英文单词 就是 设置快捷键 Alt+这个英文单词 用于菜单的 快捷 */ /********************文件菜单******************************/ QMenu * pMenuFile=new QMenu(tr("文件(&F)")); //|菜单项 this->pActionNewFile=new QAction(QIcon(":/action_imgs/resource/filenew.png"),"新建",0); //|设置快捷键 QKeySequence::New就是 Ctrl+N新建 文件 this->pActionNewFile->setShortcut(QKeySequence::New); //|设置状态栏提示 this->pActionNewFile->setStatusTip("新建一个文件"); this->pActionOpenFile=new QAction(QIcon(":/action_imgs/resource/fileopen.png"),"打开",0); this->pActionOpenFile->setShortcut(QKeySequence::Open); this->pActionOpenFile->setStatusTip("打开一个文件"); this->pActionSaveFile=new QAction(QIcon(":/action_imgs/resource/filesave.png"),"保存",0); this->pActionSaveFile->setShortcut(QKeySequence::Save); this->pActionSaveFile->setStatusTip("保存一个文件"); this->pActionSaveOtherFile=new QAction("另存为",0); //|把菜单项添加到菜单 pMenuFile->addAction(this->pActionNewFile); pMenuFile->addAction(this->pActionOpenFile); pMenuFile->addSeparator(); pMenuFile->addAction(this->pActionSaveFile); pMenuFile->addAction(this->pActionSaveOtherFile); //|为新建文件 菜单项连接信号槽 connect(pActionNewFile,SIGNAL(triggered()),this,SLOT(slotFileNewFileAction())); //|为打开文件菜单项连接信号槽 connect(this->pActionOpenFile,SIGNAL(triggered(bool)),this,SLOT(slotActionOpen())); //|为保存文件 菜单项 连接信号槽 connect(pActionSaveFile,SIGNAL(triggered(bool)),this,SLOT(slotFileSaveAction())); //|添加文件菜单到菜单条 this->ui->menuBar->addMenu(pMenuFile); /**********************编辑菜单*******************************/ QMenu * pMenuEdit=new QMenu(tr("编辑(&E)")); //|菜单项 //|添加到菜单条 this->ui->menuBar->addMenu(pMenuEdit); /**********************格式菜单******************************/ QMenu * pMenuFormat=new QMenu(tr("格式(&O)")); //|菜单项 QMenu * pMenuText=new QMenu("字体",0); this->pActionTextBold=new QAction(QIcon(":/action_imgs/resource/textbold.png"),"加粗",0); this->pActionTextBold->setStatusTip("字体加粗"); this->pActionTextBold->setCheckable(true);//|(重点)设置成可选(打钩或者不打钩) this->pActionTextItalic=new QAction(QIcon(":/action_imgs/resource/textitalic.png"),"倾斜",0); this->pActionTextItalic->setStatusTip("字体倾斜"); this->pActionTextItalic->setCheckable(true); this->pActionTextUnderLine=new QAction(QIcon(":/action_imgs/resource/textunder.png"),"下划线",0);;//|下划线 this->pActionTextUnderLine->setStatusTip("字体下划线"); this->pActionTextUnderLine->setCheckable(true); this->pActionColor=new QAction("字体颜色",0);//|字体颜色 this->pActionColor->setStatusTip("设置字体颜色"); QMenu * pMenuBlockFormat=new QMenu("段落"); /*段落对齐*/ this->pActionBlockBoth=new QAction(QIcon(":/action_imgs/resource/textjustify.png"),"两端对齐",0); this->pActionBlockBoth->setCheckable(true); this->pActionBlockBoth->setStatusTip("段落两端对齐"); this->pActionBlockCenter=new QAction(QIcon(":/action_imgs/resource/textcenter.png"),"居中对齐",0); this->pActionBlockCenter->setCheckable(true); this->pActionBlockCenter->setStatusTip("段落居中对齐"); this->pActionBlockLeft=new QAction(QIcon(":/action_imgs/resource/textleft.png"),"左对齐",0); this->pActionBlockLeft->setCheckable(true); this->pActionBlockLeft->setStatusTip("段落左对齐"); this->pActionBlockRight=new QAction(QIcon(":/action_imgs/resource/textright.png"),"右对齐",0); this->pActionBlockRight->setCheckable(true); this->pActionBlockRight->setStatusTip("段落右对齐"); //|菜单项添加到菜单 pMenuFormat->addMenu(pMenuText); pMenuText->addAction(this->pActionTextBold); pMenuText->addAction(this->pActionTextItalic); pMenuText->addAction(this->pActionTextUnderLine); pMenuFormat->addMenu(pMenuBlockFormat); pMenuBlockFormat->addAction(this->pActionBlockLeft); pMenuBlockFormat->addAction(this->pActionBlockCenter); pMenuBlockFormat->addAction(this->pActionBlockRight); pMenuBlockFormat->addAction(this->pActionBlockBoth); pMenuFormat->addAction(this->pActionColor); //|添加到菜单条 this->ui->menuBar->addMenu(pMenuFormat); //|信号槽 connect(this->pActionTextBold,SIGNAL(triggered(bool)),this,SLOT(slotTextBold())); connect(this->pActionTextItalic,SIGNAL(triggered(bool)),this,SLOT(slotsetFontItalic())); connect(this->pActionColor,SIGNAL(triggered(bool)),this,SLOT(slotsetTextColor())); connect(this->pActionTextUnderLine,SIGNAL(triggered(bool)),this,SLOT(slotSetTextUnderLine())); /********************窗口菜单*************************************/ QMenu * pMenuWindow=new QMenu(tr("窗口(&W)")); //|添加到菜单条 this->ui->menuBar->addMenu(pMenuWindow); /***********************帮助菜单**************************************/ QMenu * pMenuHelp=new QMenu(tr("帮助(&H)")); //|添加到菜单条 this->ui->menuBar->addMenu(pMenuHelp); return true; } //|新建文件 的菜单项点击槽 void MainWindow::slotFileNewFileAction() { int wndNumber=numberChild; ++numberChild; this->mCreateChildWnd("未命名文件"+QString::number(wndNumber)); } //|更新菜单项 根据Mdi的 子窗口 活动 信号 void MainWindow::UpdateMenu(QMdiSubWindow *p) { //QMessageBox::about(this,"提示","有新的子窗口活动"); } //|创建工具栏 bool MainWindow::mCreateToolBar() { /*mainToolBar为文件操作的ToolBar * SecondToolBar为文档格式更改的ToolBar * */ /*****注意:只需要在工具条上面添加Action即可(在菜单项里面的Action 自动显示Icon)********************/ /**************添加到 mainToolBar*******************************/ this->ui->mainToolBar->addAction(this->pActionNewFile); this->ui->mainToolBar->addAction(this->pActionOpenFile); this->ui->mainToolBar->addAction(this->pActionSaveFile); //字体格式 this->ui->mainToolBar->addAction(this->pActionTextBold); this->ui->mainToolBar->addAction(this->pActionTextItalic); this->ui->mainToolBar->addAction(this->pActionTextUnderLine); //段落对齐 this->ui->mainToolBar->addAction(this->pActionBlockLeft); this->ui->mainToolBar->addAction(this->pActionBlockCenter); this->ui->mainToolBar->addAction(this->pActionBlockBoth); this->ui->mainToolBar->addAction(this->pActionBlockRight); /****文档格式(美化) 工具条******/ //|段落格式 QComboBox * pBlockFormat=new QComboBox(); pBlockFormat->addItem(" 标准 "); //|字体family QFontComboBox* pFontFamilyComboBox=new QFontComboBox(); //|连接信号槽 connect(pFontFamilyComboBox,SIGNAL(activated(QString)),this,SLOT(slotActionFontFamily(QString))); //|字体大小 QComboBox * pTextWeightComboBox=new QComboBox(); pTextWeightComboBox->setStatusTip("更改字号"); //·QFontDataBase里面封装了当前操作系统支持的字体参数 QList<int> sizeint=QFontDatabase::standardSizes(); //|用循环添加到ComboBox for(int size=0;size<sizeint.length();size++) { pTextWeightComboBox->addItem(QString::number(sizeint.at(size))); } //|更改字号 连接到信号槽 connect(pTextWeightComboBox,SIGNAL(activated(QString)),this,SLOT(slotsetFontSize(QString))); //|初始化 字体大小的combobox 根据 findtext查找列表里面的值 返回索引再 setCurrentText进行设置当前选择 pTextWeightComboBox->setCurrentIndex(pTextWeightComboBox->findText(QString::number(QApplication::font().pointSize()))); //|添加到工具条 this->ui->SecondtoolBar->addWidget(pBlockFormat); this->ui->SecondtoolBar->addWidget(pFontFamilyComboBox); this->ui->SecondtoolBar->addWidget(pTextWeightComboBox); return true; } //|保存文件 菜单项 槽 void MainWindow::slotFileSaveAction() { //|获取活动子窗口指针 QMdiSubWindow * pSubWindow=this->pMdiArea->activeSubWindow(); //|判断处理 if(pSubWindow==0) { //|如果没有活动子窗口 QMessageBox::about(this,"涵涵提示","没有活动子窗口,请选择需要保存的文件所在的窗口!"); }else{ //|有活动子窗口 //|获取这个subWindow里面的子窗口部件 并且把它转化为ChildWnd* 因为本身就是ChildWnd ChildWnd * pTextEdit= (ChildWnd*)pSubWindow->widget(); if(pTextEdit->pCurrentFileUrl->isEmpty()==false) { //|如果不为空 就是之前保存过 就直接保存这个文档 //|文档对象(QTextEdit的富文本) 获取 QTextDocument * doc=pTextEdit->document(); //|文档对象写入对象 QTextDocumentWriter docwriter(*(pTextEdit->pCurrentFileUrl)); //|写入 docwriter.write(doc); //|设置标题栏 pTextEdit->mSetCurrentFileName(*(pTextEdit->pCurrentFileUrl)); //|更改文档改动标识 这样 一旦更改了 文档就会加 *号 pTextEdit->isModified=true; }else{ //|如果之前没有保存过 //|使用文件对话框 (静态函数) QString savefilename=QFileDialog::getSaveFileName(); //|获取文件名 QMessageBox::about(this,"你保存的文件为",savefilename); //|文档对象(QTextEdit的富文本) 获取 QTextDocument * doc=pTextEdit->document(); //|文档对象写入对象 QTextDocumentWriter docwriter(savefilename); //|写入 docwriter.write(doc); //|设置标题栏 pTextEdit->mSetCurrentFileName(savefilename); //|更改文档改动标识 这样 一旦更改了 文档就会加 *号 pTextEdit->isModified=true; } } } //|字体加粗 void MainWindow::slotTextBold() { QTextCharFormat cfm; //|判断QAction是否为已选择 选择 就表示加粗 否则就是 正常 加粗的值在QFont::Bold里面 //|isChecked==true为已选择 反之 未选择(有没有钩钩的显示) 需要先设置setCheckable可选择 cfm.setFontWeight(this->pActionTextBold->isChecked()?QFont::Bold:QFont::Normal); if(this->mActiveMyChild()) mActiveMyChild()->mergeCurrentCharFormat(cfm); } //|返回活动窗口指针 ChildWnd * MainWindow::mActiveMyChild() { QMdiSubWindow * pSubWindow= this->pMdiArea->activeSubWindow(); if(pSubWindow==0) return 0; return (ChildWnd*)pSubWindow->widget(); } //|打开文件 void MainWindow::slotActionOpen() { QString filepath=QFileDialog::getOpenFileName(); if(filepath.isEmpty()==true) { QMessageBox::about(this,"涵涵提示:","抱歉,你什么都没打开!"); }else{ ChildWnd * pChildWnd=new ChildWnd(); pChildWnd->mLoadFile(filepath); this->pMdiArea->addSubWindow(pChildWnd); pChildWnd->show(); } } //|字体 family void MainWindow::slotActionFontFamily(QString family) { QTextCharFormat tcf; tcf.setFontFamily(family); if(this->mActiveMyChild()) { this->mActiveMyChild()->mMergeFormatOnWordOrSelection(tcf); } } //|字体大小 void MainWindow::slotsetFontSize(QString size) { if(this->mActiveMyChild()) { int intsize=size.toInt(); QTextCharFormat tcf; tcf.setFontPointSize(intsize); this->mActiveMyChild()->mergeCurrentCharFormat(tcf); } } //|设置字体倾斜 void MainWindow::slotsetFontItalic() { bool isChicked=this->pActionTextItalic->isChecked(); QTextCharFormat tcf; tcf.setFontItalic(isChicked); if(this->mActiveMyChild()) { this->mActiveMyChild()->mergeCurrentCharFormat(tcf); } } //|设置字体颜色 void MainWindow::slotsetTextColor() { QColor color=QColorDialog::getColor(); QTextCharFormat format; format.setForeground(color); if(this->mActiveMyChild()) { this->mActiveMyChild()->mergeCurrentCharFormat(format); } } //|设置字体下划线 void MainWindow::slotSetTextUnderLine() { QTextCharFormat f; f.setFontUnderline(this->pActionTextUnderLine->isChecked()?true:false); if(mActiveMyChild()) { this->mActiveMyChild()->mergeCurrentCharFormat(f); } }
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>879</width> <height>722</height> </rect> </property> <property name="windowTitle"> <string>MyWord_V1.0内测</string> </property> <widget class="QWidget" name="centralWidget"/> <widget class="QMenuBar" name="menuBar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>879</width> <height>22</height> </rect> </property> </widget> <widget class="QToolBar" name="mainToolBar"> <attribute name="toolBarArea"> <enum>TopToolBarArea</enum> </attribute> <attribute name="toolBarBreak"> <bool>false</bool> </attribute> </widget> <widget class="QStatusBar" name="statusBar"/> <widget class="QToolBar" name="SecondtoolBar"> <property name="windowTitle"> <string>toolBar</string> </property> <attribute name="toolBarArea"> <enum>TopToolBarArea</enum> </attribute> <attribute name="toolBarBreak"> <bool>true</bool> </attribute> </widget> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
原创作品 请勿随意转载! 作者:邱于涵。QQ:1031893464
相关文章推荐
- Qt中隐藏滚动条重新实现鼠标滚轮事件wheelEvent
- Qt中对象序列化的实现
- Qt Install FrameWork——Qt打包工具
- QT 创建弹出菜单
- QT qtablewidget用法
- QT自绘最小化最大化拖动
- QNX平台下QT开发环境的配置
- Qt学习之按条件删除tableview的某一行
- debian8 编译安装为知笔记(WizQTClient)
- 《Qt Creator快速入门》(第二版,霍亚飞)自学笔记 2.2.3设置应用程序图标
- 从 Qt 的 delete 说开来
- [MQTT] 规范和资源
- QThread with movetothread
- QT中QMainWindow手动添加菜单栏以及其他控件
- 【傻瓜式】QT5+sql server(ODBC)创建数据源并且连接数据库
- Qt5.4.2(mingw)编译OSG 3.4
- Qt 自定义事件
- Qt与JS相互调用
- 模态与非模态的区别
- Qt视图加载表格