作者:_郭士铭 | 来源:互联网 | 2023-09-24 16:18
qt鼠标事件所包含的头文件,QMouseEvent中包含了两个坐标,一是全局坐标(globalPos()),即桌面坐标,这个跟Windows下的调用的getCursorPos函数
qt鼠标事件所包含的头文件,QMouseEvent中包含了两个坐标,一是全局坐标(globalPos()),即桌面坐标,这个跟Windows下的调用的getCursorPos函数得到的结果一致。
鼠标事件常见的有mousePressEvent(),mouseReleaseEvent(),mouseDoubleClickEvent(),mouseMoveEvent(),下面我就贴上几段代码吧
void Event_test::mouseMoveEvent(QMouseEvent *event) //鼠标拖动事件
{
QPoint point;
point = QCursor::pos();
qDebug()<<"point"<
qDebug()<<"move :"<<" "<pos().x()<<" "<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 "<<" "<pos().x()<<" "<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 "<<" "<pos().x()<<" "<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);
}
}