qt⿏标事件总结(坐标,跟踪,点击判断)
1、QMouseEvent中的坐标
QMouseEvent中保存了两个坐标,⼀个是全局坐标,当然另外⼀个是局部坐标。
全局坐标(globalPos())即是桌⾯屏幕坐标(screen coordinates),这个跟windows下的调⽤getCursorPos函数得到的结果⼀致。
局部坐标(pos())即是相对当前active widget的坐标,左上⾓坐标为(0, 0)。
补充⼀个公式:
this->mapFromGlobal(this->cursor().pos()) = event.pos()
2、⿏标跟踪
在qt中,⿏标跟踪对应函数mouseMoveEvent。但是,默认情况下他并不能如期象你想象的那样响应⿏标的移动。此时,你只需在合适的位置调⽤⼀下函数setMouseTracking(true)即可。
If mouse tracking is switched off, mouse move events only occur if a mouse button is pressed while the
mouse is being moved.
If mouse tracking is switched on, mouse move events occur even if no mouse button is pressed.
默认情况下,mouseMoveEvent响应你按下⿏标的某个键(拖动,但不局限于左键拖动)的⿏标移动。
3、⿏标左键拖动和左键点击的判断
⿏标左键点击很容易判断,⼀般就是在重写mousePressEvent函数,⽰例如下:
void XXXWidget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
// todo ...
}
}
左键拖动的判断⼀般放在mouseMoveEvent函数中,但是你不能向上例⼀样来判断,因为该函数的event参数总是返回Qt::NoButton。你可以这样做:
void XXXWidget::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
// todo ...
}
花好月圆歌词}
参考教材实例,增加了注释和新功能,⽅便⼤家交流学习
// mouseevent.cpp
#include "mouseevent.h"
MouseEvent::MouseEvent( ):
QMainWindow()
{
setWindowTitle(tr("Get Mouse Event"));
setCursor(Qt::CrossCursor); //设置⿏标为⼗字星
labelStatus = new QLabel();
labelStatus->setMinimumSize(500,20);
labelStatus->setFrameShape(QFrame::WinPanel);//设置标签形状
labelStatus->setFrameShadow(QFrame::Sunken); //设置标签阴影;
labelStatus->setText(tr("Mouse Position:"));
labelStatus -> setFixedWidth (100);
labelMousePos = new QLabel();
labelMousePos->setText(tr(""));
labelMousePos -> setFixedWidth (100);
statusBar()->addPermanentWidget(labelStatus);//给状态栏添加永久的部件
statusBar()->addPermanentWidget(labelMousePos);
this->setMouseTracking ( true);//⾃动跟踪⿏标(默认情况下,mouseMoveEvent响应你按下⿏标的某个键的⿏标移动。)指纹锁前十名品牌多少钱
}
MouseEvent::~MouseEvent(){
}
京东 618void MouseEvent::mouseMoveEvent ( QMouseEvent * e )//⿏标移动事件响应
{
//1.获取局部⿏标位置
labelMousePos ->setText("("+QString::number(e->x())+","+QString::number(e->y())+")");
//2.获取全局⿏标位置
QPoint coursePoint;
coursePoint = QCursor::pos();//获取当前光标的位置
labelMousePos ->setText("("+QString::number(coursePoint.x())+","+QString::number(coursePoint.y())+")"); }
void MouseEvent::mousePressEvent ( QMouseEvent * e )//⿏标单击事件响应
{
QString str="("+QString::number(e->x())+","+QString::number(e->y())+")";
if(e->button()==Qt::LeftButton){
statusBar()->showMessage (tr("Mouse Left Button Pressed:")+str);//显⽰临时信息
}
else if(e->button()==Qt::RightButton){
statusBar()->showMessage (tr("Mouse Right Button Pressed:")+str);
}
else if(e->button()==Qt::MidButton){
statusBar()->showMessage (tr("Mouse Middle Button Pressed:")+str);
}
}
void MouseEvent::mouseDoubleClickEvent( QMouseEvent * e )//⿏标双击事件响应
{ QString str="("+QString::number(e->x())+","+QString::number(e->y())+")";
if(e->button()==Qt::LeftButton){
statusBar()->showMessage (tr("Mouse Left Button Double Clicked:")+str);
}
else if(e->button()==Qt::RightButton){
statusBar()->showMessage (tr("Mouse Right Button Double Clicked:")+str);
}
else if(e->button()==Qt::MidButton){
statusBar()->showMessage (tr("Mouse Middle Button Double Clicked:")+str);
}
}
void MouseEvent::mouseReleaseEvent ( QMouseEvent * e )//⿏标松开事件响应
{
QString str="("+QString::number(e->x())+","+QString::number(e->y())+")";
statusBar()->showMessage (tr("Mouser Released:")+str,3000);
}
-------------// mouseevent.h
#ifndef MOUSEEVENT_H
#define MOUSEEVENT_H
#include
class MouseEvent : public QMainWindow
{ Q_OBJECT
public:MouseEvent();
~
MouseEvent();
private:
女士内衣品牌QLabel *labelStatus;
QLabel *labelMousePos;
protected :
void mouseMoveEvent ( QMouseEvent * e );
void mousePressEvent ( QMouseEvent * e );
void mouseReleaseEvent ( QMouseEvent * e );剑与远征溪谷遗迹
经济赔偿金void mouseDoubleClickEvent( QMouseEvent * e );
};
#endif
/
/main.cpp
#include
#include "mouseevent.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv); MouseEvent w;w.show(); ();
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论