2、Qt Project之鼠标事件监控
鼠标事件监控:
对于鼠标时间监控的相关操作,我们在简历工程的时候需要选择的是QWidget基类,不选择QMainWindow基类,如下所示:
Base class:QWidget
Step1:我们首先定义的是整个Qt软件界面的UI设计:
根据设计的情况,Qt Creator自动帮我们生成了如下的HTML脚本:
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"><class>Widget</class><widget class="QWidget" name="Widget"><property name="geometry"><rect><x>0</x><y>0</y><width>400</width><height>300</height></rect></property><property name="windowTitle"><string>Widget</string></property><widget class="QPushButton" name="pushButton"><property name="geometry"><rect><x>190</x><y>230</y><width>101</width><height>27</height></rect></property><property name="text"><string>PushButton</string></property></widget><widget class="QLabel" name="label"><property name="geometry"><rect><x>-10</x><y>200</y><width>171</width><height>17</height></rect></property><property name="font"><font><italic>true</italic><strikeout>false</strikeout></font></property><property name="text"><string> PressMove-axisEvent</string></property></widget><widget class="QLabel" name="label_2"><property name="geometry"><rect><x>0</x><y>256</y><width>171</width><height>31</height></rect></property><property name="font"><font><italic>true</italic><strikeout>false</strikeout></font></property><property name="text"><string> ReleaseMouse-axisEvent</string></property></widget><widget class="QPushButton" name="pushButton_2"><property name="geometry"><rect><x>190</x><y>200</y><width>98</width><height>27</height></rect></property><property name="text"><string>PushButton</string></property></widget><widget class="QPushButton" name="pushButton_3"><property name="geometry"><rect><x>190</x><y>260</y><width>98</width><height>27</height></rect></property><property name="text"><string>PushButton</string></property></widget><widget class="QLabel" name="label_3"><property name="geometry"><rect><x>0</x><y>226</y><width>171</width><height>31</height></rect></property><property name="font"><font><italic>true</italic><strikeout>false</strikeout></font></property><property name="text"><string> PressMouse-axisEvent</string></property></widget><widget class="QLabel" name="label_4"><property name="geometry"><rect><x>0</x><y>60</y><width>211</width><height>51</height></rect></property><property name="font"><font><pointsize>22</pointsize><italic>true</italic><underline>false</underline><strikeout>false</strikeout></font></property><property name="cursor"><cursorShape>BlankCursor</cursorShape></property><property name="text"><string>Mouse Monitor</string></property><property name="alignment"><set>Qt::AlignCenter</set></property></widget><widget class="QLabel" name="label_5"><property name="geometry"><rect><x>20</x><y>120</y><width>171</width><height>21</height></rect></property><property name="font"><font><pointsize>10</pointsize><italic>true</italic><underline>false</underline><strikeout>false</strikeout></font></property><property name="cursor"><cursorShape>BlankCursor</cursorShape></property><property name="text"><string>Designed by : mm1994uestc</string></property><property name="alignment"><set>Qt::AlignCenter</set></property></widget><widget class="QLabel" name="label_6"><property name="geometry"><rect><x>0</x><y>10</y><width>321</width><height>51</height></rect></property><property name="font"><font><pointsize>28</pointsize><italic>true</italic><underline>false</underline><strikeout>false</strikeout></font></property><property name="cursor"><cursorShape>BlankCursor</cursorShape></property><property name="text"><string>Qt interface Demo!</string></property><property name="alignment"><set>Qt::AlignCenter</set></property></widget></widget><layoutdefault spacing="6" margin="11"/><resources/><connections/> </ui>
View Code
实际的界面的效果如下所示:
Step2:我们根据设计的实际情况对每一个界面上的Button进行相关的触发机制:
a)我们需要包含Mouse鼠标相关的头文件进来
#include <QMouseEvent>
b)鼠标事件的相关函数都封装在Widget基类当中,针对上面的三类事件Event,我们将列举如下:
void Widget::mousePressEvent(QMouseEvent *e) void Widget::mouseMoveEvent(QMouseEvent *e) void Widget::mouseReleaseEvent(QMouseEvent *e)
c)这里我们需要在widget.cpp文件中对每一个事件函数进行实现:(具体实现如下所示)
void Widget::mousePressEvent(QMouseEvent *e) {qDebug() << " X , Y"; // 输出X Y字符串qDebug() << tr("%1,%2").arg(e->x()).arg(e->y()); // e是我们取得的鼠标对象,我们在对象中取得鼠标对象的相关参数,如x坐标和y坐标ui->pushButton->setText(tr("(%1,%2)").arg(e->x()).arg(e->y())); // 我们将取得的xy的坐标更新到我们的Button的内容当中 } void Widget::mouseMoveEvent(QMouseEvent *e) {ui->pushButton_2->setText(tr("%1,%2").arg(e->x()).arg(e->y())); // 与上同理 } void Widget::mouseReleaseEvent(QMouseEvent *e) {ui->pushButton_3->setText(tr("%1,%2").arg(e->x()).arg(e->y())); // 与上同理 }
d)我们需要在头文件widget.h文件当中声明我们实现的鼠标的相关函数:
protected:void mousePressEvent(QMouseEvent *);void mouseMoveEvent(QMouseEvent *);void mouseReleaseEvent(QMouseEvent *);
以上就基本完成了整个程序的所有功能,详细的工程代码请参考我博客园文件中的内容(文件名称MouseMonitor.tar.gz):https://i.cnblogs.com/Files.aspx
转载于:https://www.cnblogs.com/uestc-mm/p/8946712.html
2、Qt Project之鼠标事件监控相关推荐
- Qt实现全局鼠标事件监听器-Linux
Qt实现全局鼠标事件监听器-Linux版
- Qt实现全局鼠标事件监听器-Windows
Qt实现全局鼠标事件监听器-Windows版
- 从源码分析DEARGUI之键盘鼠标事件监控
from dearpygui.dearpygui import *# 所有事件api # get_mouse_drag_delta() # get_mouse_pos() # is_key_down( ...
- QT 事件系统之QMouseEvent鼠标事件
概述 在Qt中,鼠标事件被封装成.QMouseEvent对象,QMouseEvent中包含一些参数,用描述鼠标事件. 在QT中,鼠标事件包含:鼠标按键按下,鼠标按键释放,鼠标双击事件,鼠标移动事件. ...
- QT mouseMoveEvent事件(qmainwindow内追踪鼠标事件事件)
最近用Qt软件界面,需要用到mouseMoveEvent,研究了下,发现些问题,分享一下. 在Qt中要捕捉鼠标移动事件需要重写MouseMoveEvent,但是MouseMoveEvent为了不太耗资 ...
- qt linux 鼠标事件,QT学习笔记5:QMouseEvent鼠标事件简介
一.QMouseEvent的详细描述 首先请注意,Qt中的QMouseEvent一般只涉及鼠标左键或右键的单击.释放等操作,而对鼠标滚轮的响应则通过QWheeEvent来处理. QMouseEvent ...
- 【Qt教程】3.1 - Qt5 event事件、Label控件的鼠标事件捕获
1. event简介 事件(event)是由系统或者Qt本身在不同的时刻发出的.当用户按下鼠标.敲下键盘,或者是窗口需要重新绘制的时候,都会发出一个相应的事件.一些事件在对用户操作做出响应时发出,如键 ...
- Qt安装事件过滤器、过滤子控件事件、截获控件按键、鼠标事件
Qt安装事件过滤器.过滤子控件事件.截获控件按键.鼠标事件 Qt的事件模型一个强大的功能是一个QObject对象能够监视发送其他QObject对象的事件,在事件到达之前对其进行处理. 假设我们有一个C ...
- 不对窗体进行边框装饰后,实现对鼠标事件的监控
如果对JFrame调用了方法setUndecorated(true);,那么窗体将没有标题栏,以及标题栏上的关闭.最大化.最小化菜单了. 这是要实现对鼠标的监控就必须自己手动去添加鼠标监听.下面代码就 ...
最新文章
- 回击质疑 HP StoreOnce用高性能说话
- MySQL删除表数据
- iOS开发UI篇—手写控件,frame,center和bounds属性
- python一些常用函数_【python】常用的一些内置函数
- spring @order控制对象的顺序
- 《Objective-c》-(第一个OC程序)
- Docker生产环境技术栈有哪些
- 实习成长之路:面试官说的MySQL高可用-------主备一致到底是什么?
- Asp.Net随机中文汉字验证码
- 高中数学解析几何求轨迹常用的六种解题方法(实用干货)
- JAVA基于《美国当代英语语料库COCA词频20000》PDF提取的纯单词文件
- [LuoguP2403][SDOI2010]所驼门王的宝藏
- 无法安装冰点还原_冰点还原精灵能用Windows10吗
- jenkins使用python plugin编写代码构建时报错:Caused: java.io.IOException: Cannot run program “python“
- 常微分方程编程基础(ODE)
- linux 开源视频剪辑,Linux 上的开源视频剪辑软件Olive
- pes2017服务器维护时间,PES2017授权详情与球场数据包发布时间
- 静态库路径配置g2o
- Outlook邮箱无法登录
- 基于Java办公自动化系统(OA)的设计技术及开发环境