当前位置:网站首页>Qt | 鼠标事件和滚轮事件 QMouseEvent、QWheelEvent
Qt | 鼠标事件和滚轮事件 QMouseEvent、QWheelEvent
2022-07-25 09:32:00 【InfoQ】
前言:
鼠标事件:
- void mousePressEvent(QMouseEvent *event); //鼠标按键按下
- void mouseReleaseEvent(QMouseEvent *event); //鼠标按键抬起
- void mouseDoubleClickedEvent(QMouseEvent *event); //鼠标按键双击
- void mouseMouveEvent(QMouseEvent *event); //鼠标移动
- void wheelEvent(QWheelEvent *event); //鼠标滚轮滚动
鼠标移动事件:
- 鼠标的移动事件mouseMouveEvent()默认是在鼠标按键按下时移动鼠标的时候才会产生;
- 如果不想按下鼠标也可以获取到鼠标移动事件的话,就需要在构造函数中添加:
- setMouseTracking(true);//设置鼠标跟踪
设置鼠标形状:
QCursor cursor;
cursor.setShape(Qt::OpenHandCursor);//小手掌形状
setCursor(cursor);
//或者使用:QApplication::setOverriedCursor(cursor);//使鼠标指针暂时改变形状
QCursor cursor("./image/logo.png");
setCursor(cursor);
//或者使用:QApplication::setOverrideCursor(cursor);//使鼠标指针暂时改变形状
Q_UNUSED():
滚轮事件:
- QWheelEvent()类的delta()函数返回了滚轮移动的距离。
- 每当滚轮转动一下,默认是15度,这时调用QWheelEvent()::delta()返回的值就是15*8=120。
- 转动方向为向外,返回正值;转动方向为向内,返回负值。所以可以通过这个函数的返回值的正负来判断滚轮的方向。
实现鼠标拖动窗口移动:
void Widget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton) // 鼠标左键
{
m_isMouseLeftDown = true;
m_dragPos = event->globalPos() - pos(); //获取指针位置和窗口位置的差值
}
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)//见注:
{
move(event->globalPos() - m_dragPos);
event->accept(); //事件处理函数“接收”了这个事件,不要再向父部件传递;
}
}
实现滚轮放大或缩小编辑器中的内容:
void Widget::wheelEvent(QWheelEvent *event)
{
if(event->delta() > 0)
{
ui->textEdit->zoomIn();//放大
}
else
{
ui->textEdit->zoomOut();//缩小
}
}边栏推荐
猜你喜欢

About the jar package of slf4j log4j log4j2 used together

Configuration of OSPF protocol (take Huawei ENSP as an example)

mongoDB的使用

2.shell脚本之条件语句

5.NFS共享服务和ssh远程控制服务

Reproduce asvspoof 2021 baseline rawnet2

Mysql5.7主从数据库部署(离线部署)

Trojang attack on neural networks paper reading notes

2021 牛客网笔试总结 02

5. This simple "echo" usage, can the child next door!
随机推荐
Selenium waits for the occurrence of elements and the conditions under which the waiting operation can be performed
Pytorch tensor list is converted to tensor list of tensor to tensor using torch.stack()
VS Code 连接远程 Jupyter 服务器
1、 The difference between unittest framework and pytest framework
2.shell脚本之条件语句
4、 Testfixture test fixture, or test firmware
Voxceleb1 dataset Download
2021 牛客网笔试总结 01
使用Three.js实现炫酷的赛博朋克风格3D数字地球大屏
JS encryption parameter positioning
Fastdfs offline deployment (Graphic)
Attention is all you need paper intensive reading notes transformer
Yiwen society, three necessary packet capturing tools for hackers
Ansible Deployment Guide
2. Introduce the deployment of lamp platform +discuz Forum
9.shell文本处理三剑客之awk
Deadlock event of thread pool
Vscode latex workshop set xelatex compilation
Differences between redis and mongodb
mongoDB的使用