一、图形界面应用程序的消息处理模型
二、Qt的事件处理
1、Qt平台将系统产生的消息转换为Qt事件(每一个系统消息对象Qt平台的一个事件)
(1)、Qt事件是一个QEvent的对象
(2)、Qt事件用于描述程序内部或者外部发生的动作
(3)、任意的QObject对象都具备事件处理的能力
2、GUI应用程序的事件处理方式
(1)、Qt事件产生后立即被分发到QWidget对象
(2)、QWidget中的event(QEvent*)进行事件处理
(3)、event()根据事件类型调用不同的事件处理函数
(4)、在事件处理函数中发送Qt中预定义的信号
(5)、调用信号关联的槽函数
3、情景分析:按钮点击
(1)、接收到鼠标事件(代表一个系统消息)
(2)、调用event(QEvent*)成员函数
(3)、调用MouseReleaseEvent(QMouseEvent*)成员函数
(4)、调用clicked()成员函数
(5)、触发信号SIGNAL(clicked())
4、事件(QEvent)和信号(SIGNAL)不同
(1)、事件由具体对象进行处理
(2)、信号由具体对象主动产生
(3)、改写事件处理函数可能导致程序行为发生改变
(4)、信号是否存在对应的槽函数不会改变程序的行为
(5)、一般而言,信号在具体的事件处理函数中产生
三、文本编辑器的关闭操作
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
void MainWindow::closeEvent(QCloseEvent *e)//没有对应的信号来处理,只能重写事件处理函数 { preEditorChanged(); if(!m_isTextChanged) { QMainWindow::closeEvent(e);//调用父类的关闭事件处理函数 } else { e->ignore();//点取消的话就忽略这个对话框 } }
完整代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMenuBar> #include <QMenu> #include <QAction> #include <QString> #include <QtGui/QMainWindow> #include <QToolBar> #include <QIcon> #include <QSize> #include <QStatusBar> #include <QLabel> #include <QPlainTextEdit> #include <QFileDialog> class MainWindow : public QMainWindow { Q_OBJECT private: QPlainTextEdit mainEdit; QLabel statusLabel; QString m_filePath;//记得在构造函数里初始化 bool m_isTextChanged;//构造函数里初始化为false MainWindow(QWidget *parent = 0); MainWindow(const MainWindow& obj); MainWindow* operator = (const MainWindow& obj); bool construct(); bool initMenuBar();//菜单栏 bool initToolBar();//工具栏 bool initStatusBar();//状态栏 bool initinitMainEditor();//编辑窗口 bool initFileMenu(QMenuBar* mb);//文件菜单 bool initEditMenu(QMenuBar* mb);//编辑菜单 bool initFormatMenu(QMenuBar* mb);//格式菜单 bool initViewMenu(QMenuBar* mb);//视图菜单 bool initHelpMenu(QMenuBar* mb);//帮助菜单 bool initFileToolItem(QToolBar* tb);//工具选项 bool initEditToolItem(QToolBar* tb); bool initFormatToolItem(QToolBar* tb); bool initViewToolItem(QToolBar* tb); bool makeAction(QAction*& action,QMenu* menu, QString text, int key);//菜单项 bool makeAction(QAction*& action,QToolBar* tb, QString tip, QString icon); QString showFileDialog(QFileDialog::AcceptMode mode, QString title);//文件对话框 void showErrorMessage(QString message);//错误消息对话框 int showQuesstionMessage(QString message);//问题消息对话框 QString saveCurrentData(QString path = ""); void preEditorChanged(); private slots: void onFileNew(); void onFileOpen(); void onFlieSave(); void onFileSaveAs(); void onTextChanged(); protected: void closeEvent(QCloseEvent *e);//重写关闭窗口的事件处理函数 public: static MainWindow* NewInstance(); ~MainWindow(); }; #endif // MAINWINDOW_H
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include "MainWindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), statusLabel(this) { m_filePath = ""; m_isTextChanged = false; setWindowTitle("NotePad-[New]"); } bool MainWindow::construct() { bool ret = true; ret = ret && initMenuBar(); ret = ret && initToolBar(); ret = ret && initStatusBar(); ret = ret && initinitMainEditor(); return ret; } MainWindow* MainWindow::NewInstance() { MainWindow* ret = new MainWindow(); if((ret==NULL) || (!ret->construct())) { delete ret; ret = NULL; } return ret; } bool MainWindow::initMenuBar()//菜单栏 { bool ret = true; QMenuBar* mb = menuBar();//一定要注意是menuBar(),这是普通成员函数,不是构造函数 ret = ret && initFileMenu(mb);//传一个参数是为了在initFileMenu()函数将menu加入菜单栏 ret = ret && initEditMenu(mb); ret = ret && initFormatMenu(mb); ret = ret && initViewMenu(mb); ret = ret && initHelpMenu(mb); return ret; } bool MainWindow::initToolBar()//工具栏 { bool ret = true; QToolBar* tb = addToolBar("Tool Bar"); //tb->setMovable(false); //tb->setFloatable(false); tb->setIconSize(QSize(16,16)); ret = ret && initFileToolItem(tb); tb->addSeparator(); ret = ret && initEditToolItem(tb); tb->addSeparator(); ret = ret && initFormatToolItem(tb); tb->addSeparator(); ret = ret && initViewToolItem(tb); return ret; } bool MainWindow::initStatusBar()//状态栏 { bool ret = true; QStatusBar* sb = statusBar(); QLabel* label = new QLabel("Made By LGC"); if(label != NULL) { statusLabel.setMinimumWidth(200); statusLabel.setAlignment(Qt::AlignHCenter); statusLabel.setText("Ln:1 Col:1"); label->setMinimumWidth(200); label->setAlignment(Qt::AlignHCenter); sb->addPermanentWidget(new QLabel());//单纯加入分隔符 sb->addPermanentWidget(&statusLabel); sb->addPermanentWidget(label); } else { ret = false; } return ret; } bool MainWindow::initinitMainEditor()//编辑窗口 { bool ret = true; mainEdit.setParent(this); setCentralWidget(&mainEdit); connect(&mainEdit, SIGNAL(textChanged()), this, SLOT(onTextChanged())); return ret; } /************************************************文件菜单********************************************************/ bool MainWindow::initFileMenu(QMenuBar* mb) { bool ret = true; QMenu* menu = new QMenu("File(&F)");//创建文件菜单,(&F)是为了可以Alt+F打开 ret = (menu != NULL); if(ret) { QAction* action = NULL; //New ret = ret && makeAction(action, menu, "New(&N)",Qt::CTRL + Qt::Key_N); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFileNew())); menu->addAction(action); } menu->addSeparator(); //Open ret = ret && makeAction(action, menu,"Open(&O)...",Qt::CTRL + Qt::Key_O); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFileOpen())); menu->addAction(action); } menu->addSeparator(); //Save ret = ret && makeAction(action, menu,"Save(&S)",Qt::CTRL + Qt::Key_S); if(ret) { connect(action, SIGNAL(triggered()), this ,SLOT(onFlieSave())); menu->addAction(action); } menu->addSeparator(); //Save As ret = ret && makeAction(action, menu, "Save As(&A)...",0); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFileSaveAs())); menu->addAction(action); } menu->addSeparator(); //print ret = ret && makeAction(action, menu, "Print(&P)...",Qt::CTRL + Qt::Key_P); if(ret) { menu->addAction(action); } menu->addSeparator(); //Exit ret = ret && makeAction(action, menu,"Exit(&X)",0); if(ret) { menu->addAction(action);//将菜单项加入到菜单 } } if(ret) { mb->addMenu(menu);//将菜单加入到菜单栏 } else { delete mb; } return ret; } /************************************************编辑菜单********************************************************/ bool MainWindow::initEditMenu(QMenuBar* mb) { bool ret = true; QMenu* menu = new QMenu("Edit(&E)"); ret = (menu != NULL); if(ret) { QAction* action = NULL; //Undo ret = ret && makeAction(action, menu,"Undo(&U)",Qt::CTRL + Qt::Key_Z); if(ret) { menu->addAction(action); } menu->addSeparator(); //Redo ret = ret && makeAction(action, menu,"Redo(&R)...",Qt::CTRL + Qt::Key_Y); if(ret) { menu->addAction(action); } menu->addSeparator(); //Cut ret = ret && makeAction(action, menu,"Cut(&T)",Qt::CTRL + Qt::Key_X); if(ret) { menu->addAction(action); } menu->addSeparator(); //Copy ret = ret && makeAction(action, menu,"Copy(&C)...",Qt::CTRL + Qt::Key_C); if(ret) { menu->addAction(action); } menu->addSeparator(); //Pase ret = ret && makeAction(action, menu,"Pase(&P)...",Qt::CTRL + Qt::Key_V); if(ret) { menu->addAction(action); } menu->addSeparator(); //Delete ret = ret && makeAction(action, menu, "Delete(&L)",Qt::Key_Delete); if(ret) { menu->addAction(action); } menu->addSeparator(); //Find ret = ret && makeAction(action, menu,"Find(&F)...",Qt::CTRL + Qt::Key_F); if(ret) { menu->addAction(action); } menu->addSeparator(); //Replace ret = ret && makeAction(action, menu,"Replace(&R)...",Qt::CTRL + Qt::Key_H); if(ret) { menu->addAction(action); } menu->addSeparator(); //Goto ret = ret && makeAction(action, menu,"Goto(&G)",Qt::CTRL + Qt::Key_G); if(ret) { menu->addAction(action); } menu->addSeparator(); //Select All ret = ret && makeAction(action, menu, "Select All(&A)",Qt::CTRL + Qt::Key_A); if(ret) { menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret; } /************************************************格式菜单********************************************************/ bool MainWindow::initFormatMenu(QMenuBar* mb) { bool ret = true; QMenu* menu = new QMenu("Format(&O)"); ret = (menu != NULL); if(ret) { QAction* action = NULL; //Auto Wrap ret = ret && makeAction(action, menu,"Auto Wrap(&W)",0); if(ret) { menu->addAction(action); } menu->addSeparator(); //Font ret = ret && makeAction(action, menu,"Font(&F)...",0); if(ret) { menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret; } /************************************************视图菜单********************************************************/ bool MainWindow::initViewMenu(QMenuBar* mb) { bool ret = true; QMenu* menu = new QMenu("View(&V)"); ret = (menu != NULL); if(ret) { QAction* action = NULL; //Tool Bar ret = ret && makeAction(action, menu,"Tool Bar(&T)",0); if(ret) { menu->addAction(action); } menu->addSeparator(); //Status Bar ret = ret && makeAction(action, menu,"Status Bar(&S)",0); if(ret) { menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret; } /************************************************帮助菜单********************************************************/ bool MainWindow::initHelpMenu(QMenuBar* mb) { bool ret = true; QMenu* menu = new QMenu("Help(&H)"); ret = (menu != NULL); if(ret) { QAction* action = NULL; //User Manual ret = ret && makeAction(action, menu,"User Manual",0); if(ret) { menu->addAction(action); } menu->addSeparator(); //About NotePad ret = ret && makeAction(action, menu,"About NotePad...",0); if(ret) { menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret; } /*****************************************工具************************************************************/ bool MainWindow::initFileToolItem(QToolBar* tb) { bool ret = true; QAction* action = NULL; ret = ret && makeAction(action, tb, "New", ":/Res/pic/new.png"); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFileNew())); tb->addAction(action); } ret = ret && makeAction(action, tb,"Open", ":/Res/pic/open.png"); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFileOpen())); tb->addAction(action); } ret = ret && makeAction(action, tb,"Save", ":/Res/pic/save.png"); if(ret) { connect(action, SIGNAL(triggered()), this ,SLOT(onFlieSave())); tb->addAction(action); } ret = ret && makeAction(action, tb,"Save As", ":/Res/pic/saveas.png"); if(ret) { connect(action, SIGNAL(triggered()), this, SLOT(onFileSaveAs())); tb->addAction(action); } ret = ret && makeAction(action, tb,"Print", ":/Res/pic/print.png"); if(ret) { tb->addAction(action); } return ret; } bool MainWindow::initEditToolItem(QToolBar* tb) { bool ret = true; QAction* action = NULL; ret = ret && makeAction(action, tb,"Undo", ":/Res/pic/undo.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, tb,"Redo", ":/Res/pic/redo.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, tb, "Cut", ":/Res/pic/cut.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, tb,"Copy", ":/Res/pic/copy.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, tb,"Paste", ":/Res/pic/paste.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, tb,"Find", ":/Res/pic/find.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, tb,"Replace", ":/Res/pic/replace.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, tb,"Goto", ":/Res/pic/goto.png"); if(ret) { tb->addAction(action); } return ret; } bool MainWindow::initFormatToolItem(QToolBar* tb) { bool ret = true; QAction* action = NULL; ret = ret && makeAction(action, tb, "Auto Wrap", ":/Res/pic/wrap.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, tb,"Font", ":/Res/pic/font.png"); if(ret) { tb->addAction(action); } return ret; } bool MainWindow::initViewToolItem(QToolBar* tb) { bool ret = true; QAction* action = NULL; ret = ret && makeAction(action, tb,"Tool Bar", ":/Res/pic/tool.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, tb,"Status Bar", ":/Res/pic/status.png"); if(ret) { tb->addAction(action); } return ret; } bool MainWindow::makeAction(QAction*& action,QMenu* menu, QString text, int key)//菜单项 { bool ret = true; action = new QAction(text, menu); if(action != NULL) { action->setShortcut(QKeySequence(key));//创建快捷键 } else { ret = false; } return ret; } bool MainWindow::makeAction(QAction*& action,QToolBar* tb, QString tip, QString icon) { bool ret = true; action = new QAction("", tb); if(action != NULL) { action->setToolTip(tip); action->setIcon(QIcon(icon)); } else { ret = false; } return ret; } MainWindow::~MainWindow() { }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <QFileDialog> #include <QStringList> #include <QFile> #include <QDebug> #include <QMessageBox> #include "MainWindow.h" #include <QMap> QString MainWindow::showFileDialog(QFileDialog::AcceptMode mode, QString title) { QString ret = ""; QFileDialog fd; QStringList filters; QMap<QString, QString> map; const char* fileArray[][2]= { {"Text(*.txt)", ".txt"}, {"All Files(*.*)", "*" }, {NULL, NULL} }; for(int i=0; fileArray[i][0] != NULL; i++) { filters.append(fileArray[i][0]); map.insert(fileArray[i][0], fileArray[i][1]); } fd.setWindowTitle(title); fd.setAcceptMode(mode); fd.setNameFilters(filters); if(mode==QFileDialog::AcceptOpen) { fd.setFileMode(QFileDialog::ExistingFile); } if(fd.exec()==QFileDialog::Accepted) { ret = fd.selectedFiles()[0]; QString posix = map[fd.selectedNameFilter()];//把下拉中选中的后缀对应键值取出 if(posix != "*" && !ret.endsWith(posix)) { ret += posix; } } return ret; } void MainWindow::showErrorMessage(QString message) { QMessageBox mb(this); mb.setWindowTitle("Quession"); mb.setText(message); mb.setIcon(QMessageBox::Critical); mb.setStandardButtons(QMessageBox::Ok); mb.exec(); } int MainWindow::showQuesstionMessage(QString message) { QMessageBox mb(this); mb.setWindowTitle("Error"); mb.setText(message); mb.setIcon(QMessageBox::Question); mb.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); return mb.exec(); } QString MainWindow::saveCurrentData(QString path) { QString ret = path; if(ret == "") { ret = showFileDialog(QFileDialog::AcceptSave, "Save"); } if(ret != "") { QFile file(ret); if(file.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream out(&file); out << QString(mainEdit.toPlainText()); file.close(); setWindowTitle("NotePad - [" + ret + "]"); m_isTextChanged = false;//保存后修改状态值 } else { showErrorMessage(QString("Open file Error!\n\n") + "\"" + m_filePath + "\""); ret = ""; } } return ret; } void MainWindow::preEditorChanged() { if(m_isTextChanged) { int r = showQuesstionMessage("Do you want to Save?"); switch (r) { case QMessageBox::Yes: saveCurrentData(m_filePath); break; case QMessageBox::No: m_isTextChanged = false; break; case QMessageBox::Cancel: break; } } } void MainWindow::onFileNew() { preEditorChanged(); if(!m_isTextChanged) { mainEdit.clear(); m_filePath = ""; m_isTextChanged = false; setWindowTitle("NotePad-[New]"); } } void MainWindow::onFileOpen() { preEditorChanged(); if(!m_isTextChanged) { QString path = showFileDialog(QFileDialog::AcceptOpen, "open"); if(path != "") { QFile file(path); if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { mainEdit.setPlainText(QString(file.readAll())); file.close(); m_filePath = path;//报存当前文件路径 setWindowTitle("NotePad - [" + m_filePath + "]"); } else { showErrorMessage(QString("Open file Error!\n\n") + "\"" + m_filePath + "\""); } } } } void MainWindow::onFlieSave() { QString path = saveCurrentData(m_filePath); if(path != "") { m_filePath = path; } } void MainWindow::onFileSaveAs() { QString path = saveCurrentData();//使用默认参数 if(path != "") { m_filePath = path; } } void MainWindow::onTextChanged() { if(!m_isTextChanged) { setWindowTitle("*" + windowTitle()); } m_isTextChanged = true; } void MainWindow::closeEvent(QCloseEvent *e)//没有对应的信号来处理,只能重写事件处理函数 { preEditorChanged(); if(!m_isTextChanged) { QMainWindow::closeEvent(e);//调用父类的关闭事件处理函数 } else { e->ignore();//点取消的话就忽略这个对话框 } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <QtGui/QApplication> #include "MainWindow.h" #include <QTextCodec> int main(int argc, char *argv[]) { QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK")); //路径名支持中文 QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK")); //QString支持中文 QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK")); //string支持中文 QApplication a(argc, argv); MainWindow* w = MainWindow::NewInstance(); int ret = -1; if(w != NULL) { w->show(); ret = a.exec(); } delete w; return ret; }
四、小结
(1)、事件(QEvent)和信号(SIGNAL)不同
(2)、事件由QObject对象进行处理
(3)、信号由QObject对象触发
(4)、重写事件处理函数可能改变程序行为
(5)、信号的触发不会对程序行为造成影响
(6)、事件处理是在实际工程开发中的应用非常普遍