2015-10-06 68 views
0

我在我的UI中有一个带有图像的QScrollArea,我希望在点击图像时获得一些价值。如何通过鼠标事件拖动获得价值

更明确的是,我需要改变图像的亮度,我会用鼠标得到的值。我已经看到MouseMoveEvent,但我不知道如何使用它。

如果我单击并拖动时获得鼠标的位置,则可以提取一个值以更改我的图像的亮度,这一点我知道。我只是不知道我将如何得到这个位置。

有谁知道我能做到这一点?

Ps .:我的QScrollArea创建于Design,所以我没有任何我写的规范QScrollArea的规范。

+1

向我们展示一些代码。你有什么尝试? –

+0

我没有这方面的任何内容,我试图把一个QScrollBar放在QSrollArea里面,也许当我滚动鼠标时图像会改变。我可以做到这一点,但这不是我想要的,导致scrollBar一直出现,只有当我点击ScrollBar时才会有效,所以我没有任何代码显示,对不起:s – alitalvez

回答

0

您需要的所有信息都在发送到您的小部件的mouseMoveEvent处理程序的QMouseEvent对象中。

QMouseEvent::buttons()
QMouseEvent::pos()

一个简单的方法做你追求的是每当你收到一个“鼠标移动事件”和QMouseEvent对象报告一个按钮可改变图像的亮度(这意味着用户在按住按钮的同时移动鼠标)。

void MyWidget::mousePressEvent(QMouseEvent* event) 
{ 
    if (event->button() == Qt::LeftButton) 
    { 
     // Keep the clicking position in some private member of type 'QPoint.' 

     m_lastClickPosition = event->pos(); 
    } 
} 


void MyWidget::mouseMoveEvent(QMouseEvent* event) 
{ 
    // The user is moving the cursor. 
    // See if the user is pressing down the left mouse button. 

    if (event->buttons() & Qt::LeftButton) 
    { 
     const int deltaX = event->pos().x() - m_lastClickPosition.x(); 
     if (deltaX > 0) 
     { 
      // The user is moving the cursor to the RIGHT. 
      // ... 
     } 
     else if (deltaX < 0) // This second IF is necessary in case the movement was all vertical. 
     { 
      // The user is moving the cursor to the LEFT. 
      // ... 
     } 
    } 
} 
+0

我昨天发现如何获取我想要的鼠标位置,漂亮的样子。所以,这应该工作两个,无论如何。 – alitalvez

+0

你好。你可以在这里回答你自己的问题,所以下次你可以为自己写一个答案并选择它作为解决方案。这似乎是一种鼓励性的做法。问候。 –