当前位置:网站首页>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();
}
}
边栏推荐
- QT常用全局宏定义
- 史上最全的Redis基础+进阶项目实战总结笔记
- When custom annotations implement log printing, specific fields are blocked from printing
- 半自动化爬虫-爬取一个网站的内容及回复
- 研发团队数字化转型实践
- 理财产品的月年化收益率怎么算?
- 自定义注解实现日志打印时屏蔽特定字段不打印
- 在码云拉取代码后,调整了seata版本1.5.2。出现如下异常。是因为数据库表缺少字段导致的吗?
- QLineEdit学习与使用
- Detailed explanation of the working principle of crystal oscillator
猜你喜欢
随机推荐
插入排序 优化插入排序
研发团队数字化转型实践
Sftp中文件名乱码
TCP百万并发服务器优化调参
OnePlus 10RT appears on Geekbench, product launch also seems to be approaching
GTK修改pixmap像素,提取pixmap像素RGB值
网上开户佣金万一靠谱吗,网上开户安全吗
【100个网络运维工作者必须知道的小知识!】
SQL的索引详细介绍
金仓数据库 KingbaseES V8.3 至 V8.6 迁移最佳实践(4. V8.3 到 V8.6 数据库移植实战)
Live tonight!
02 es cluster construction
Vulnhub target drone: HARRYPOTTER_ NAGINI
SQL的ROUND函数用法及其实例
GridControl helper class for DevExpress
GRUB2的零日漏洞补丁现已推出
变量交换;复合赋值;增递减运算符
完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
opencv real-time face detection
金仓数据库KingbaseES安全指南--6.3. Kerberos身份验证









