本文介绍了Qt mouseMoveEvent只有当鼠标左键被按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个绘制线条和矩形的程序。

  void mousePressEvent(QMouseEvent * event); 
void mouseReleaseEvent(QMouseEvent * event);
void mouseMoveEvent(QMouseEvent * event);

我使用mouseMoveEvent绘制一条线的临时预览,当我释放我绘制实际线。我想知道的是,只有当我按下鼠标左键时,我如何才能使mouseMoveEvent工作。我尝试了以下操作,但是整个功能停止工作。

  void mouseMoveEvent(QMouseEvent * event)
{
if(event-> button()== Qt :: LeftButton)
{
//做东西
}
}

但是该函数没有任何作用。任何帮助将非常感谢

解决方案

QMouseEvent :: button()

您应该使用按钮()

  if(event-> buttons()& Qt :: LeftButton)
{
// do stuff
}


I currently have a program that draws lines and rectangles.

void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);

I use mouseMoveEvent to draw temporary preview of a line and when i release i draw the actual line. What I would like to know is how can i make mouseMoveEvent work work only when i have the left mouse button pressed down. I tried the following but then the whole function stops working.

void mouseMoveEvent(QMouseEvent *event)
{
     if(event->button() == Qt::LeftButton)
     {
        //do stuff
     }
}

but then the function doesn't do anything. Any assistance would be much appreciated

解决方案

From the documentation of QMouseEvent::button():

You should use buttons() instead.

if(event->buttons() & Qt::LeftButton)
{
    //do stuff
}

这篇关于Qt mouseMoveEvent只有当鼠标左键被按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 19:03