[QT]获取鼠标坐标以及按键响应

您所在的位置:网站首页 qt获取鼠标的位置 [QT]获取鼠标坐标以及按键响应

[QT]获取鼠标坐标以及按键响应

2023-10-22 23:35| 来源: 网络整理| 查看: 265

原创文章

原文链接:https://blog.csdn.net/humanking7/article/details/80707591

文章目录 @[toc]1.原理1.1效果2.代码2.1.鼠标事件代码2.2.功能性代码3.注意3.1. 设置Mouse Tracking3.2. 当鼠标在窗口之外如何获取屏幕位置4.源码下载地址1.原理

重写GUI类中的鼠标事件的响应函数,并实现其响应函数。

要包含头文件#include

1.1效果IMG20180615000002IMG201806150000022.代码2.1.鼠标事件代码

在头文件中声明

protected: //mouse void mouseMoveEvent(QMouseEvent *event); //移动 void mousePressEvent(QMouseEvent *event); //单击 void mouseReleaseEvent(QMouseEvent *event); //释放 void mouseDoubleClickEvent(QMouseEvent *event); //双击 void wheelEvent(QWheelEvent *event); //滑轮

在源文件中实现

void check_keyboard_mouse::mouseMoveEvent(QMouseEvent *event) {//移动 QPoint p_ab = event->globalPos(); QPoint p_re = event->pos(); QString str; str = QString("%1 , %2").arg(p_ab.x()).arg(p_ab.y()); ui.edit_m_absolute->setText(str); str = QString("%1 , %2").arg(p_re.x()).arg(p_re.y()); ui.edit_m_relative->setText(str); } void check_keyboard_mouse::mousePressEvent(QMouseEvent *event) {//单击 // 如果是鼠标左键按下 if (event->button() == Qt::LeftButton){ //qDebug() button() == Qt::RightButton){ //qDebug() button() == Qt::MidButton){ //qDebug() button() == Qt::LeftButton){ //qDebug() button() == Qt::RightButton){ //qDebug() delta(); setMouseState(MouseState::Wheel, wheel_val); // 当滚轮远离使用者时 //if (wheel_val > 0){ // qDebug() setStyleSheet(style_active); break; case R_DC: ui.lab_mR_D->setStyleSheet(style_active); break; case Wheel: ui.lab_mM_val->setText(QString("%1").arg(wheelVal)); if (wheelVal>0) {// 当滚轮远离使用者时 ui.lab_mM_up->setStyleSheet(style_active); ui.lab_mM_down->setStyleSheet(style_release); } else { ui.lab_mM_up->setStyleSheet(style_release); ui.lab_mM_down->setStyleSheet(style_active); } break; case Release: //setMouseUIdefault(); break; } } void check_keyboard_mouse::setMouseUIdefault() { QString style_release = "border:2px solid black;"; ui.lab_mR_D->setStyleSheet(style_release); ui.lab_mR->setStyleSheet(style_release); ui.lab_mL_D->setStyleSheet(style_release); ui.lab_mL->setStyleSheet(style_release); ui.lab_mM->setStyleSheet(style_release); ui.lab_mM_up->setStyleSheet(style_release); ui.lab_mM_down->setStyleSheet(style_release); }3.注意3.1. 设置Mouse Tracking

如果想触发mouseMoveEvent()这个鼠标移动的响应函数,则必须要设置窗体(或控件)是可以Mouse Tracking的,不然程序不会进入mouseMoveEvent()函数。

void setMouseTracking(bool enable)

This property holds whether mouse tracking is enabled for the widget. If mouse tracking is disabled (the default), the widget only receives mouse move events when at least one mouse button is pressed while the mouse is being moved. If mouse tracking is enabled, the widget receives mouse move events even if no buttons are pressed.

check_keyboard_mouse::check_keyboard_mouse(QWidget *parent) : QDialog(parent) { ui.setupUi(this); this->setMouseTracking(true);//设置窗体可响应 Mouse Move ui.edit_m_relative->setMouseTracking(true);//控件1 ui.edit_m_absolute->setMouseTracking(true);//控件2 ui.edit_keyValue->setMouseTracking(true);//控件3 ui.edit_keyValue_Hex->setMouseTracking(true);//控件4 ui.edit_keyValue_QtKey->setMouseTracking(true);//控件5 }3.2. 当鼠标在窗口之外如何获取屏幕位置

当鼠标移动出窗体,鼠标就无法进入mouseMoveEvent()函数,此时就需要通过其他方式获取鼠标在屏幕上的位置信息。

这里用到了这个QCursor类,这个类中有一个获取鼠标当前屏幕位置(绝对位置)的一个静态方法。

static QPoint QCursor::pos(const QScreen * screen)

我在这里的处理方式是,通过按键盘的Ctrl键,获取当前的鼠标的绝对位置,需要实现键盘的按键响应函数,已经在之前的博客中写到。

void check_keyboard_mouse::keyPressEvent(QKeyEvent *event) { qDebug() key(); if (keyValue == Qt::Key_Control) {//获取鼠标位置 QPoint pt = QCursor::pos();//获取鼠标的绝对位置 QString str; str = QString("%1 , %2").arg(pt.x()).arg(pt.y()); ui.edit_m_absolute->setText(str);//显示 } }4.源码下载地址

基本上,上述代码就可以实现,最好大家自己动手研究一下,利于自己掌握,不过也给大家提供源码: 下载点这里



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3