当前位置:网站首页>QT_Event class
QT_Event class
2022-08-01 17:38:00 【little engineer】
常用事件
事件
QWidget Classes can handle a wide variety of events,Here are some commonly used event handler functions.
窗口事件:
virtual void closeEvent(QCloseEvent *event); // 关闭
virtual void showEvent(QShowEvent *event); // 显示
virtual void hideEvent(QHideEvent *event); // 隐藏
virtual void moveEvent(QMoveEvent *evnet); // 移动
virtual void resizeEvent(QResizeEvent *event); // 改变大小
这里通过QMoveEvent The following member functions of the class can get the old and new coordinates of the window:
const QPoint &oldPos() const; // 旧坐标
const QPoint &newPos() constl // 新坐标
通过QResizeEvent The following member functions of the class can get the old and new size of the window:
const QSize &oldSize() const; // 旧大小
const QSize &newSize() const; // 新大小
键盘事件:
virtual void keyPressEvent(QKeyEvent *event); // 键按下
virtual void keyReleaseEvent(QKeyEvent *event); // 键松开
这里通过QKeyEvent The member functions of the class can get some information about the keys,如:
int key() const; // 得到键值
鼠标事件:
virtual void mousePressEvent(QMouseEvent *event); // 鼠标键按下
virtual void mouseReleaseEvent(QMouseEvent *event); // 鼠标键松开
virtual void mouseDoubleCllckEvent(QMouseEvent *event); // mouse button double click
virtual void mouseMoveEvent(QMouseEvent *event); // 鼠标移动
virtual void enterEvent(QEvent *event); // 鼠标进入窗口
virtual void leaveEvent(QEvent *event); // 鼠标离开窗口
virtual void wheelEvent(QWheelEvent *event); // Mouse wheel moves
这里通过QMouseEvent Event member functions to obtain information about the mouse,如:
const QPoint &pos() const; // 得到鼠标坐标(Relative to the window that received the event)
int x() const; // Get the abscissa of the mouse(Relative to the window that received the event)
int y() const; // Get the mouse ordinate(Relative to the window that received the event)
const QPoint &globalPos() const; // 得到鼠标坐标(全局坐标)
int globalX() const; // Get the abscissa of the mouse(全局坐标)
int globalY() const; // Get the mouse ordinate(全局坐标)
Qt::MouseButton button() const; // Get the mouse button that caused the event
Qt::MouseButtons buttons() const; // Get the mouse button state when the event occurred
其中,Qt::MouseButton 是一个枚举类型,There are the following common values.
1)Qt::NoButton: 无键.
2)Qt::LeftButton: 左键.
3)Qt::RightButton:右键.
4)Qt::MidButton: 中键.
注意,对于鼠标移动事件QMouseEvent 和button 函数总是返回Qt::NoButton,而buttonsThe function return value isQt::MouseButton 类型的“按位或” 组合,It reflects the pressed state of the mouse button when the event occurs.
QWheelEvent Class representing scroll wheel events,It has a set withQMountEvent Classes have almost identical member functions,但少一个button 函数,Add the following two functions:
int delta() const; // Get the angle that the wheel is turned
Qt::Orientation orientationI() const; // Gets the direction the wheel is turning
其中Qt::Orientation 是一个枚举类型,它有以下取值.
1)Qt::Horizontal: 横向.
2)Qt::Vertical: 纵向.
焦点事件:
virtual void focusInEvent(QFocusEvent *event); // 获得焦点
virtual void focusOutEvent(QFocusEvent *event); // 失去焦点
None of these event handlers return a value,So if you want to accept or reject and an event to callQEvent 类的成
员函数,如:
event->accept(); // 接受事件
event->ignore(); // 拒绝事件
The outcome of an event being rejected is on a case-by-case basis,Such as after the close event is rejected,The window will not be closed,而键盘、Input events such as mouse are rejected and propagate up to the parent window.
键盘事件
Capture keyboard and mouse events even when the widget has focus,You may not get key events either,Because other windows may catch keyboard events.A window that captures keyboard events will get all keyboard events,And other windows will not get keyboard events at all,Until the window that captured the keyboard event releases the keyboard event.The member functions related to keyboard event capture are as follows:
void grabKeyboard(); // 捕获键盘事件
void releaseKeyboard(); // Release keyboard event
Similar to the capture and release of mouse events,Its member functions are as follows:
void grabMouse(); // 捕获鼠标事件
void releaseMouse(); // Release mouse event
The capture of keyboard events and mouse events is independent of each other.这里要注意两点:
One is if another window has been captured,The window currently in the capture state will lose the capture of the event;
The second is that only visible windows can capture input events.
The following member functions get the window in the application that is capturing keyboard or mouse events:
QWidget *keyboardGrabber(); // Get the window that is capturing keyboard events
QWidget *mouseGrabber(); // Get the window that is capturing mouse events
These two functions are static functions.
定时事件
1. A single timing operation
startTimer(40); //40MS刷新一次
void XVideoUI::timerEvent(QTimerEvent* e)
{
if (pressSlider)return; //When the progress bar is dragged,Do not refresh the timer,防止冲突
double pos = XVideoThread::Get()->GetPos(); //获取到位置
ui.playSlider->setValue(pos * 1000); //Convert to thousandths
}
2.judging time eventsID
timer = startTimer(1000);
void Widget::timerEvent(QTimerEvent *event)
{
if(event->timerId() == timer)
{
readFrame();
update();
}
}
killTimer(timer); //结束定时
3.事件槽
QTimer *timer=new QTimer(this); //新建一个定时器对象
connect(timer,SIGNAL(timeout()),this,SLOT(showTime())); //(d)
timer->start(1000); //(e)
绘图事件
程序产生事件有两种方式,一种是调用QApplication::postEvent(), 例如QWidget::update()函数,当需要重新绘制屏幕时,程序调用update()函数,放入Qt的消息队列中,等待依次被处理;postEvent()中事件对象的生命期由Qt 平台管理,只支持分配在堆上的事件对象,事件被处理后由Qt 平台销毁.
另一种方式是调用sendEvent() 函数, 事件不会放入队列, 而是直接被派发和处理,QWidget::repaint()函数用的就是阻塞型的.sendEvent()中事件对象的生命期由Qt 程序管理,支持分配在栈上和堆上的事件对象;
#include <QPainter>
void Widget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
}
鼠标事件
#include <QMouseEvent>
setWindowFlags(Qt::FramelessWindowHint); //(b) 无边框,标题栏
setWindowOpacity(0.5); //(c) 窗体透明度0.5
void DigiClock::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
dragPosition=event->globalPos()-frameGeometry().topLeft();//保存当前鼠标点所在的位置相对于窗体左上角的偏移值
event->accept();
}
if(event->button()==Qt::RightButton)
{
close();
}
}
void DigiClock::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons()&Qt::LeftButton)
{
move(event->globalPos()-dragPosition);//moveThe parameter refers to the position of the upper left corner of the form!!
event->accept();
}
}
边栏推荐
猜你喜欢
下载 | 谷歌科学家Kevin P. Murphy发布新书《概率机器学习:高级主题》
史上最全的Redis基础+进阶项目实战总结笔记
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
QPalette调色板、框架色彩填充
创造建材数字转型新视界,中建材如何多边赋能集团业务快速发展
QT常用全局宏定义
【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)
MySql 怎么查出符合条件的最新的数据行?
Shell nl命令详解(显示行号、读取文件)
opencv语法Mat类型总结
随机推荐
C语言理论--笔试面试基础稳固
2022年SQL经典面试题总结(带解析)
实现mnist手写数字识别
星途一直缺颠覆性产品?青岛工厂这款M38T,会是个突破点?
tooltip control
tooltip 控件
MySql 怎么查出符合条件的最新的数据行?
08 Spark cluster construction
The site is not found after the website is filed. You have not bound this domain name or IP to the corresponding site! The configuration file does not take effect!
频域分析实践介绍
OpenCV安装、QT、VS配置项目设置
创造建材数字转型新视界,中建材如何多边赋能集团业务快速发展
关于MySql中explain结果filtered的理解
个人日记
表达式;运算符,算子;取余计算;运算符优先顺序
【R语言】批量重命名文件
面经汇总-社招-6年
金仓数据库 OCCI迁移指南(3. KingbaseES的OCCI特性支持)
计算IoU(D2L)
GTK修改pixmap像素,提取pixmap像素RGB值