Qt Events 示例代码
来源:互联网 发布:linux 文件系统初始化 编辑:程序博客网 时间:2024/06/15 21:53
Qt Events
Fold
Table of Contents
Objectives
Qt Events
Difference between events and signal/slots
Key Events
Timer Events
Role of events in painting
Event Filters
Event Propagation
Post/Send Events
Custom Events
Objectives
- Understand the concept of events
- Understand the difference between events and signal/slots
- Be able to implement event handling
- Understand the role of events in painting
- Understand the role of event filters and be able to use them
- Understand the concept of event propagation
- Learn how to post/send events
- Be able to create/send/handle custom events
Qt Events
- In Qt, events are objects that represent things that have happened either within an application or as a result of outside activity that the application needs to know about.
- When an event occurs, Qt creates an event object to represent it and delivers it to a particular instance of QObject (or a subclass) by calling its {{event()} function.
- This function calls an event handler for the specific event.
- Three categories:
- Spontaneous events are generated by the window system. They are put in a system queue and processed one after the other by the event loop.
- Posted events are generated by Qt or by the application. They are queued by Qt and processed by the event loop.
- Sent events are generated by Qt or by the application, but they are sent directly to the target object.
- Events are useful when we write our own widgets or we want to modify the behavior of existing widgets.
Difference between events and signal/slots
- Events and signal/slots are two parallel mechanisms that can accomplish the same thing.
- An event will be generated by an outside entity and will be delivered through the event loop in QApplication.
- Signal/slots will be used by the interclass communication
- You handle events but you get notified by signal emissions.
- Signals are useful when using a widget, whereas events are useful when implementing a widget.
Key Events
- Key events are handled by re-implementing keyPressEvent() and keyReleaseEvent().
- Key names used by Qt
Example: Handling the ESC key.
keypress.h
#ifndef KEYPRESS_H#define KEYPRESS_H #include <QWidget>#include <QtGui> class KeyPress : public QWidget{ Q_OBJECTpublic: KeyPress(QWidget *parent = 0); protected: void keyPressEvent(QKeyEvent *); void keyReleaseEvent(QKeyEvent *); private: QLabel *myLabel; QVBoxLayout *mainLayout;}; #endif // KEYPRESS_H
keypress.cpp
#include "keypress.h" #include <QApplication>#include <QKeyEvent> KeyPress::KeyPress(QWidget *parent) : QWidget(parent){ myLabel = new QLabel("LABEL"); mainLayout = new QVBoxLayout; mainLayout->addWidget(myLabel); setLayout(mainLayout); } void KeyPress::keyPressEvent(QKeyEvent *event){ if(event->key() == Qt::Key_Escape) { myLabel->setText("You pressed ESC"); }} void KeyPress::keyReleaseEvent(QKeyEvent *event){ if(event->key() == Qt::Key_Escape) { myLabel->setText("You released ESC"); }}
main.cpp
#include <QtGui>#include "keypress.h" int main(int argc, char *argv[]){ QApplication a(argc, argv); KeyPress *keyPress = new KeyPress(); keyPress->show(); return a.exec();}
Timer Events
- Timer events allow applications to perform processing at regular time intervals
- The QTimer class provides an interface for timers. To use it:
- Create a QTimer
- connect its timeout() signal to the appropriate slots
- call start()
- From then on it will emit the timeout() signal at constant intervals.
Example: Simple Counter
mainwindow.h
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QtGui> #include <QMainWindow> class MainWindow : public QWidget{ Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: QPushButton *myButton; QLabel *myLabel; QTimer *myTimer; int timerCount; private slots: void changeText(); void timerTimeout(); }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"#include "ui_mainwindow.h" #include <QTime> MainWindow::MainWindow(QWidget *parent) : QWidget(parent){ myButton = new QPushButton("Start Timer"); myLabel = new QLabel("100"); QVBoxLayout *myLayout = new QVBoxLayout; myLayout->addWidget(myButton); myLayout->addWidget(myLabel); setLayout(myLayout); myTimer = new QTimer(this); myTimer->setInterval(10); timerCount = 0; connect(myButton, SIGNAL(clicked()), this, SLOT(changeText())); connect(myTimer, SIGNAL(timeout()), this, SLOT(timerTimeout())); } MainWindow::~MainWindow(){ } void MainWindow::changeText(){ myButton->setEnabled(false); timerCount = 100; if(!myTimer->isActive()) myTimer->start(); }void MainWindow::timerTimeout(){ myLabel->setText(QString::number(timerCount)); --timerCount; if (timerCount <= 0) { myTimer->stop(); myButton->setEnabled(true); myLabel->setText(QString::number(100)); } }
main.cpp
#include <QtGui/QApplication>#include "mainwindow.h" int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; w.adjustSize(); w.move(QApplication::desktop()->screen()->rect().center() - w.rect().center()); w.show(); return a.exec();}
Role of events in painting
- There are situations, when we need to create a widget from scratch. In such a case, we need to do painting.
- The QPainter class is instrumental when we do some painting in the Qt4 programming library. The paining is done with the QPainter class inside the paintEvent() method.
- paintEvent() is called whenever the widget needs to be repainted. Every widget displaying custom content must implement it. Painting using a QPainter can only take place in a paintEvent() or a function called by a paintEvent().
Event Filters
a QObject instance can
- A QObject instance can be set to monitor the events of another QObject instance before the latter object even sees them.
- Setting up an event filter involves two steps:
- Register the monitoring object with the target object by calling installEventFilter() on the target.
- Handle the target object’s events in the monitor’s eventFilter() function
Example: Filter numbers from QLineEdit.
KeyboardFilter.h
#ifndef KEYBOARDFILTER_H#define KEYBOARDFILTER_H #include <QApplication>#include <QLineEdit>#include <QKeyEvent> class KeyboardFilter : public QObject{public: KeyboardFilter( QObject *parent = 0 ) : QObject( parent ) {} protected: bool eventFilter( QObject *dist, QEvent *event );}; #endif // KEYBOARDFILTER_H
keyboardFilter.cpp
#include "KeyboardFilter.h" bool KeyboardFilter::eventFilter( QObject *dist, QEvent *event ){ if( event->type() == QEvent::KeyPress ) { QKeyEvent *keyEvent = static_cast<QKeyEvent*>( event ); if( QString("1234567890").indexOf( keyEvent->text() ) != -1 ) return true; } return false;}
main.cpp
#include <QtGui>#include "KeyboardFilter.h" int main( int argc, char **argv ){ QApplication app( argc, argv ); QLineEdit lineEdit; KeyboardFilter filter; lineEdit.installEventFilter( &filter ); lineEdit.show(); return app.exec();}
Event Propagation
Event propagation is when an event is passed to the base class.
Post/Send Events
- Qt applications rarely need to call postEvent() or sendEvent() directly because most events are generated automatically by Qt or by the window system
- Most of the times Qt includes a function that does it for you. For example, update() and repaint().
Custom Events
- Qt lets you create your own event types
- Custom types can be useful in single-threaded applications, as an inter-object communication mechanism
- A custom event can be handled by an object. To make a custom event you have to:
- Implement this class's custom event handler (customEvent(…)), so that when an event is sent to an instance of this class, this method will be called to handle the situation.
- Send an event using QApplication::postEvent(…) and give an instance of the class mentioned before as one of the parameters.
阅读全文
1 0
- Qt Events 示例代码
- QT示例代码【积累】
- QT QStringListModel 示例代码
- Qt QSortFilterProxyModel示例代码, 使用方法
- 事件处理(Handling Events)和委托(Delegate)代码示例(一)【UE4】【C++】
- 事件处理(Handling Events)和委托(Delegate)代码示例(二)【UE4】【C++】
- 事件处理(Handling Events)和委托(Delegate)代码示例(三)【UE4】【C++】
- Qt QFileSystemModel QDirModel 示例代码, 使用方法
- C#与QT操作office示例代码
- QT C++实现多线程通信--示例代码
- QT: Threads, Events and QObjects
- Events and Event Filters in Qt
- [译|Qt]Threads, Events and QObjects
- ubuntu12.04 + Qt + glut + C 环境搭建和示例代码
- Qt中信号与槽的简单代码片段示例
- qt 滑动滑块 ,进度条跟着滑动变化 代码示例
- Qt学习: QCloseEvent关闭事件的使用及代码示例
- 使用Qt示例代码实现的视频播放。VS项目
- Android 缓存
- JavaScript基本概念
- Java反射机制详解
- 页面js定义一定要用var而非let
- MVC5 razor视图通过JavaScript读取Controller中的集合类
- Qt Events 示例代码
- jQuery中的end()
- HDU1176:免费馅饼(DP) (G)
- Apache Shiro会话超时,登录页面跳转到父窗体的解决方法
- [leetcode]49. Group Anagrams
- 一张表读懂ON(安森美)
- iOS开发自动检测内存泄漏神器MLeaksFinder
- was集群架构设计
- SpringBoot启动过程