QT学习小结之鼠标事件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QT学习小结之鼠标事件相关的知识,希望对你有一定的参考价值。
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);
}
}
本文出自 “11699921” 博客,请务必保留此出处http://11709921.blog.51cto.com/11699921/1787760
以上是关于QT学习小结之鼠标事件的主要内容,如果未能解决你的问题,请参考以下文章