第38课 Qt中的事件处理(上)
1. GUI程序原理回顾
(1)图形界面应用程序的消息处理模型
(2)思考:操作系统发送的消息如何转变为Qt信号
2. Qt中的事件处理
(1)Qt平台将系统产生的消息转换为Qt事件
①Qt事件是一个QEvent的对象
②Qt事件用于描述程序内部或外部发生的动作
③任意的QObject对象都具备事件处理的能力
(2)GUI应用程序的事件处理方式
①Qt事件产生后立即被分发到QWidget对象
②调用QWidget::event(QEvent*)进行事件处理
③event()根据事件类型的不同,调用不同的事件处理函数
④在事件处理函数中发送Qt中预定义的信号
⑤调用信号关联的槽函数
(3)QPushButton事件处理分析
①接收到鼠标事件
②QApplication调用QObject::event(QEvent*)成员函数,进行事件的分派。
③调用mouseReleaseEvent(QMouseEvent*)成员函数
④QPushButton调用click()成员函数
⑤触发信号SIGNAL(clicked())
【编程实验】自定义事件处理函数
//main.cpp
#include "Widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
//QMyPushButton.h
#ifndef _QMYPUSHBUTTON_H_ #define _QMYPUSHBUTTON_H_ #include <QPushButton> typedef void (QButtonListener)(QObject*,QMouseEvent*); class QMyPushButton : public QPushButton { Q_OBJECT protected: QButtonListener* m_listener; //重写QPushButton的事件处理函数 void mouseReleaseEvent(QMouseEvent *e); public: , QButtonListener* listener = ); }; #endif // _QMYPUSHBUTTON_H_
//QMyPushButton.cpp
#include "QMyPushButton.h" #include <QMouseEvent> QMyPushButton::QMyPushButton(QWidget* parent, QButtonListener* listener):QPushButton(parent) { m_listener = listener; } //重写改写事件处理函数,会改变程序的行为。 void QMyPushButton::mouseReleaseEvent(QMouseEvent *e) { if(m_listener != NULL) { //调用自定义的事件处理函数,尽管按钮的clicked信号被连接到onMyButtonClicked槽函数, //但因自定义的m_listener函数里并不触发clicked信号,从而槽函数不会被调用。 m_listener(this, e); e->accept();//事件被接收,就不再传递到父QWidget setDown(false); //按钮设置为“弹起”状态 } else { //父类的mouseReleaseEvent会去调用clicked(),并触发SIGNAL(clicked()) //从而调用到连接到该信号的槽函数(本例为onMyButtonClicked()) QPushButton::mouseReleaseEvent(e); //调用父类 } }
//Widget.h
#ifndef _WIDGET_H_ #define _WIDGET_H_ #include <QWidget> #include "QMyPushButton.h" class Widget : public QWidget { Q_OBJECT QMyPushButton myButton; protected slots: void onMyButtonClicked(); public: Widget(QWidget *parent = ); ~Widget(); }; #endif // _WIDGET_H_
//Widget.cpp
#include "Widget.h" #include <qDebug> //自定义事件处理函数 void onMyButtonMouseRelease(QObject* sender, QMouseEvent* e) { qDebug() << "onMyButtonMouseRelease(QObject* sender, QMouseEvent* e)"; } Widget::Widget(QWidget *parent) : QWidget(parent),myButton(this, onMyButtonMouseRelease) //实验2:myButton(this, 0) { myButton.setText("QMyPushButton"); connect(&myButton, SIGNAL(clicked()), this, SLOT(onMyButtonClicked())); } //槽函数,用于接收按钮的clicked信号 void Widget::onMyButtonClicked() { qDebug() << "onMyButtonClicked()" ; } Widget::~Widget() { }
(4)事件(QEvent)和信号(SIGNAL)的不同
事件(QEvent) |
信号(SIGNAL) |
|
与QObject的关系 |
由具体对象进行处理 |
由具体对象主动产生 |
对程序影响 |
改写事件处理函数可能导致程序行为发生改变 |
信号是否存在对应的槽函数不会改变程序行为 |
两者的联系 |
一般而言,信号在具体的事件处理函数中产生 |
3. 文本编辑器中的关闭操作
【编程实验】文本编辑器的关闭操作
//修改的部分
//MainWindow.h …… protected: void closeEvent(QCloseEvent *e); //重写窗体的关闭事件 …… //MainWindowSlots.cpp void MainWindow::closeEvent(QCloseEvent* e) { preEditorChanged(); if(!m_isTextChanged) { QMainWindow::closeEvent(e); } else { //当用户按下“取消” e->ignore();//忽略关闭事件 } }
//完整代码(只列出MainWindow.h和MainWindowSlots.cpp,其余文件与上一个NotePad程序一样)
//MainWindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QMenuBar> //#include <QKeySequence> //#include <QAction> #include <QPlainTextEdit> #include <QLabel> #include <QFileDialog> class MainWindow : public QMainWindow { Q_OBJECT private: QPlainTextEdit mainEditor; QLabel statusLbl; QString m_filePath;//当前操作的文件路径 bool m_isTextChanged; //标识编辑框中的内容是否改变 //将构造函数、复制构造、赋值函数等私有化 MainWindow(QWidget *parent = ); MainWindow(const MainWindow&); MainWindow& operator= (const MainWindow&); bool construct(); //二阶构造模式 bool initMenuBar(); //初始化菜单栏 bool initToolBar(); //初始化工具栏 bool initStatusBar(); //初始化状态栏 bool initMainEditor();//初始化文本编辑组件 //菜单设置 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, QWidget* parent, QString text, int key); //生成工具栏中的各按钮 bool makeAction(QAction*& action, QWidget* parent, QString tip, QString icon); QString showFileDialog(QFileDialog::AcceptMode mode, QString title);//显示打开和保存对话框 void showErrorMessage(QString message);//显示“错误对话框”(QMessagBox) int showQueryMessage(QString message); //显示“询问对话框” QString saveCurrentData(QString path = "", QString title = "Save"); //保存编辑框中的内容 void preEditorChanged(); //判断文本框是否被更改,并决定是否弹出“保存对话框” protected: void closeEvent(QCloseEvent *e); //重写窗体的关闭事件 private slots: void onFileNew(); void onFileOpen(); void onFileSave(); void onFileSaveAs(); void onTextChanged();//文本框内容发生改变里,会收到textChanged信号,这里是接收该信号的槽函数 public: static MainWindow* NewInstance(); ~MainWindow(); }; #endif // MAINWINDOW_H
//MainWindowSlots.cpp
//该文件MainWindowSlots.cpp与MainWindowUI.cpp的分离 //体现了界面和功能代码分离的思想 #include "MainWindow.h" #include <QMessageBox> #include <QFile> #include <QTextStream> #include <QMap> #include <QCloseEvent> #include <QDebug> void MainWindow::showErrorMessage(QString message) { QMessageBox msg(this); msg.setWindowTitle("Erro"); msg.setText(message); msg.setIcon(QMessageBox::Critical); msg.setStandardButtons(QMessageBox::Ok); msg.exec(); } int MainWindow::showQueryMessage(QString message) { QMessageBox msg(this); msg.setWindowTitle("Query"); msg.setText(message); msg.setIcon(QMessageBox::Question); msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); return msg.exec(); } QString MainWindow::showFileDialog(QFileDialog::AcceptMode mode, QString title) { QString ret = ""; QFileDialog fd(this); QStringList filters; QMap<QString, QString> map; ] = { {"Text(*.txt)", ".txt"}, {"All Files(*.*)", "*" }, {NULL, NULL} }; ; filterArray[i][] != NULL; i++) { filters.append(filterArray[i][]); map.insert(filterArray[i][], filterArray[i][]); } fd.setWindowTitle(title); fd.setAcceptMode(mode); //QFileDialog::AcceptOpen或AcceptSave fd.setNameFilters(filters); if(mode == QFileDialog::AcceptOpen) { fd.setFileMode(QFileDialog::ExistingFile); //打开文件必须存在! } if(fd.exec() == QFileDialog::Accepted) { ret = fd.selectedFiles()[]; //Qt5中ret返回的是完整的路径名,含后缀。因此,后面的if块可省略,但Qt4可能 //会返回不带后缀的文件名,当保存文件时,须手动加上去。 if(mode == QFileDialog::AcceptSave) { QString postfix = map[fd.selectedNameFilter()]; if((postfix != "*") && !ret.endsWith(postfix)) { ret = ret + postfix; } } } return ret; } void MainWindow::preEditorChanged() { if (m_isTextChanged) { int r = showQueryMessage("Do you want to save the changes to file?"); 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) { mainEditor.clear(); setWindowTitle("NotePad - [ New ]"); m_filePath = ""; m_isTextChanged = false; } } 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)) { mainEditor.setPlainText(QString(file.readAll())); file.close(); m_filePath = path; //记录当前打开的文件路径和文件名 m_isTextChanged = false; setWindowTitle("NotePad - [" + m_filePath + "]"); } else { showErrorMessage(QString("Open file Error!\n\n") + "\"" + path + "\""); } } } } QString MainWindow::saveCurrentData(QString path, QString title) { QString ret = path; if (ret =="") { //执行下面语句时,用户可以点击“取消”,此时ret返回"" ret = showFileDialog(QFileDialog::AcceptSave, title); } if (ret != "") { QFile file(ret); if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream out(&file); out << mainEditor.toPlainText(); file.close(); setWindowTitle("NotePad - [" + ret + "]"); m_isTextChanged = false; //己保存 } else { showErrorMessage(QString("Save file Error!\n\n") + "\"" + m_filePath + "\""); ret =""; //保存失败时 } } return ret; } void MainWindow::onFileSave() { QString path = saveCurrentData(m_filePath, "Save"); if( path != "" ) { m_filePath = path; } } void MainWindow::onFileSaveAs() { QString path = saveCurrentData(m_filePath, "Save As"); if( path != "" ) { m_filePath = path; } } void MainWindow::onTextChanged() { if( !m_isTextChanged ) { setWindowTitle("*" + windowTitle()); } m_isTextChanged = true; //qDebug()<< "onTextChanged()"; } void MainWindow::closeEvent(QCloseEvent* e) { preEditorChanged(); if(!m_isTextChanged) { QMainWindow::closeEvent(e); } else { //当用户按下“取消” e->ignore();//忽略关闭事件 } }
4. 小结
(1)Qt中的事件和信号不同
(2)事件由QObject对象进行处理
(3)信号由QObject对象触发
(4)重写事件处理函数可能改变程序行为
(5)信号的触发不会对程序行为造成影响
(6)事件处理是在实际工程开发中应用非常普遍。
第38课 Qt中的事件处理(上)的更多相关文章
- 第39课 Qt中的事件处理(下)
1. 事件的传递过程 (1)操作系统检测到用户动作时,会产生一条系统消息,该消息被发送到Qt应用程序 (2)Qt应用程序收到系统消息后,将其转化为一个对应的QEvent事件对象,并调用QObject: ...
- Qt 中的事件处理(一)
1.图形界面应用程序的消息处理模型 特点: 基于操作系统才能运行 GUI应用程序提供的功能必须由用户触发 用户操作界面时操作系统是第一个感知的 系统内核的消息通过事件处理转变成QT的信号 2. Qt中 ...
- Qt中在图片上叠加显示文字
Qt中在图片上叠加显示文字 QCustLabel::QCustLabel(QWidget *parent):QLabel(parent){ setPixmap(QPixmap(QString::f ...
- Qt 中的事件处理(二)
1. 回顾事件传递的过程 ①源头:操作系统 操作系统检测到用户的动作时,就会产生一个系统消息,系统消息就会被发送到正在运行的Qt应用程序中, ②应用程序收到系统消息后, 他会将系统消息翻译成对应的 ...
- Qt事件系统之一:Qt中的事件处理与传递
一.简介 在Qt中,事件作为一个对象,继承自 QEvent 类,常见的有键盘事件 QKeyEvent.鼠标事件 QMouseEvent 和定时器事件 QTimerEvent 等,与 QEvent 类的 ...
- 第47课 Qt中的调色板
1. QPalette类 (1)QPalette类提供了绘制QWidget组件的不同状态所使用的颜色. (2)QPalette对象包含了3个状态的颜色描述 ①激活颜色组(Active):组件获得焦点使 ...
- 第30课 Qt中的文本编辑组件
1. 3种常用的文本编辑组件的比较 单行文本支持 多行文本支持 自定义格式支持 富文本支持 QLineEdit (单行文本编辑组件) Yes No No No QPlainTextEdit (多行普通 ...
- 第11课 Qt中的字符串类
1. 历史遗留问题和解决方案 (1)历史遗留问题 ①C语言不支持真正意义上的字符串 ②C语言用字符数组和一组函数实现字符串操作 ③C语言不支持自定义类型,因此无法获得字符串类型 (2)解决方案 ①从C ...
- 第7课 Qt中的坐标系统
1. 坐标系统 (1)GUI操作系统都有特定的坐标系统 (2)图形界面程序在坐标系统中进行窗口和部件的定位 (3)定位类型 ①顶级窗口部件的定位 ②窗口内部件的定位 ③窗口部件的大小设置 (4)QWi ...
随机推荐
- thinkphp怎么设置输入网址直接进入首页
1.设置apache服务器的时候,文件的位置要写包含index.php的那个主文件夹. 2.apache和thinkphp默认index.html是作为网站的默认首页,所以在浏览器端可以直接输入网址进 ...
- 使用NPOI读取Excel到DataTable
一.NPOI介绍: 使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写.NPOI是构建在POI 3.x版本之上的,它可以在没有安装Office ...
- J2EE的13种核心技术
一.JDBC(Java Database Connectivity) JDBC API为访问不同的数据库提供了一种统一的途径,象ODBC一样,JDBC对开发者屏蔽了一些细节问题,另外,JDBC对数据库 ...
- UEditor编辑器的使用
1.首先我们要去官网下载UEditor编辑器,选择语言,这里我用的是php utf-8版本(李昌辉) 2.下载完成之后解压文件,将解压的文件放到我们的网站目录里面 3.ueditor/utf8-php ...
- CSS3与页面布局学习笔记(四)——页面布局大全(负边距、双飞翼、多栏、弹性、流式、瀑布流、响应式布局)
一.负边距与浮动布局 1.1.负边距 所谓的负边距就是margin取负值的情况,如margin:-100px,margin:-100%.当一个元素与另一个元素margin取负值时将拉近距离.常见的功能 ...
- 我和Ajax的故事
我和Ajax结缘是在2015年的3月份,当时的项目需要Ajax技术来实现,但对于我来说完全是全新的名词,自己就上网上查找相关资料,结局很明显,知道概念但是具体的是什么东西根本傻傻不明白,后来这个技术是 ...
- JavaScript基本语法(四)
一. JavaScript 函数 1.函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块.我们可以将一些常用的代码封装成函数,待用到的时候就能直接调用使用.利用函数可以使代码的组织结构 ...
- php每天一题:strlen()与mb_strlen()的作用分别是什么
strlen()与mb_strlen()都是用于获取字符串长度的,那么它们两个有什么不同? strlen()与mb_strlen()的不同之处在于mb_strlen()第二个参数可以用于指定字符编码. ...
- [Android]Android端ORM框架——RapidORM(v1.0)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4748077.html Android上主流的ORM框架有很多 ...
- fragment 监听返回
@Override public void onResume() { super.onResume(); getView().setFocusableInTouchMode(true); getVie ...