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



原创文章,欢迎转载。转载请注明:转载自 祥的博客

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


文章目录

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

1.原理

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

要包含头文件#include

1.1效果

[QT]获取鼠标坐标以及按键响应_第1张图片

2.代码

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() << "left click";
		setMouseState( MouseState::L_C, 0);
	}
	// 如果是鼠标右键按下
	else if (event->button() == Qt::RightButton){
		//qDebug() << "right click";
		setMouseState(MouseState::R_C, 0);
	}
	else if (event->button() == Qt::MidButton){
		//qDebug() << "mid click";
		setMouseState(MouseState::M_C, 0);
	}

}

void check_keyboard_mouse::mouseReleaseEvent(QMouseEvent *event)
{//释放
	setMouseState(MouseState::Release, 0);
}

void check_keyboard_mouse::mouseDoubleClickEvent(QMouseEvent *event)
{//双击
	// 如果是鼠标左键按下
	if (event->button() == Qt::LeftButton){
		//qDebug() << "left double click";
		setMouseState(MouseState::L_DC, 0);
	}
	else if (event->button() == Qt::RightButton){
		//qDebug() << "right double click";
		setMouseState(MouseState::R_DC, 0);
	}
}

void check_keyboard_mouse::wheelEvent(QWheelEvent *event)
{//滚轮
	int wheel_val = event->delta();
	setMouseState(MouseState::Wheel, wheel_val);	
	// 当滚轮远离使用者时
	//if (wheel_val > 0){
	//	qDebug() << "wheel forward: " << wheel_val;
	//}
	//else{//当滚轮向使用者方向旋转时
	//	qDebug() << "wheel backward: " << wheel_val;
	//}
}


2.2.功能性代码

enum MouseState //自己定义的状态类型
{
	L_C,//left click
	R_C,
	M_C,
	L_DC,//left double click
	R_DC,
	Wheel,//wheel move
	Release
};

void check_keyboard_mouse::setMouseState(MouseState ms, int wheelVal)
{
	QString style_active = "border:2px solid black;background:green";
	QString style_release = "border:2px solid black;";
	setMouseUIdefault();
	switch (ms)
	{
	case L_C:
		ui.lab_mL->setStyleSheet(style_active);
		break;
	case R_C:
		ui.lab_mR->setStyleSheet(style_active);
		break;
	case M_C:
		ui.lab_mM->setStyleSheet(style_active);
		break;
	case L_DC:
		ui.lab_mL_D->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() << "=== press key";
	int keyValue = event->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.源码下载地址

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


赞赏码New

你可能感兴趣的:(Qt,Qt实用Trick)