如何通过鼠标事件拖动获取值

时间:2015-10-06 19:19:40

标签: c++ qt user-interface qscrollarea

我在我的UI中有一个QScrollArea带有图像,我想在点击图像时获得一些价值。

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

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

有谁知道我该怎么做?

Ps:我的QScrollArea是在Design上创建的,因此我没有使用QScrollArea的规格来编写任何代码。

2 个答案:

答案 0 :(得分:0)

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

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.
            // ...
        }
    }
}

答案 1 :(得分:0)

有一个小的更正。当我这样编程时,我的动作有点生涩。所以我试图修改代码,现在工作完美。 继承人我如何改变

   const int deltaX = event->scenePos().x() - m_lastClickPosition.x();
    if ( deltaX > 10 )// to the right
    {
       moveBy(10,0);
       m_lastClickPosition = event->scenePos();
    }
    else if ( deltaX <-10 )
    {
        moveBy(-10,0);
        m_lastClickPosition = event->scenePos();
    }
相关问题