qt鼠标事件所包含的头文件<QMouseEvent>,QMouseEvent中包含了两个坐标,一是全局坐标(globalPos()),即桌面坐标,这个跟Windows下的调用的getCursorPos函数得到的结果一致。
鼠标事件常见的有mousePressEvent(),mouseReleaseEvent(),mouseDoubleClickEvent(),mouseMoveEvent(),下面我就贴上几段代码吧
void Event_test::mouseMoveEvent(QMouseEvent *event) //鼠标拖动事件
{
QPoint point;
point = QCursor::pos();
qDebug()<<"point"<<point.x()<<" "<<point.y();
qDebug()<<"move :"<<" "<<event->pos().x()<<" "<<event->pos().y();
}
void Event_test::mousePressEvent(QMouseEvent *event) //鼠标按下事件
{
QString str="("+QString::number(event->x())+","+QString::number(event->y())+")";
if(event->button()==Qt::LeftButton)
{
statusBar()->showMessage (tr("Mouse Left Button Pressed:")+str);//在状态栏中显示临时信息
}
else if(event->button()==Qt::RightButton)
{
statusBar()->showMessage (tr("Mouse Right Button Pressed:")+str);
}
else if(event->button()==Qt::MidButton)
{
statusBar()->showMessage (tr("Mouse Middle Button Pressed:")+str);
}
qDebug()<<"Press "<<" "<<event->pos().x()<<" "<<event->pos().y();
}
void Event_test::mouseReleaseEvent(QMouseEvent *event) //鼠标释放事件
{
QString str="("+QString::number(event->x())+","+QString::number(event->y())+")";
statusBar()->showMessage (tr("Mouser Released:")+str,3000);
qDebug()<<"release "<<" "<<event->pos().x()<<" "<<event->pos().y();
}
void Event_test::mouseDoubleClickEvent(QMouseEvent *event) //鼠标双击事件
{
QString str="("+QString::number(event->x())+","+QString::number(event->y())+")";
if(event->button()==Qt::LeftButton)
{
statusBar()->showMessage (tr("Mouse Left Button Double Clicked:")+str);
}
else if(event->button()==Qt::RightButton)
{
statusBar()->showMessage (tr("Mouse Right Button Double Clicked:")+str);
}
else if(event->button()==Qt::MidButton)
{
statusBar()->showMessage (tr("Mouse Middle Button Double Clicked:")+str);
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。