如何通过鼠标事件拖动获取值
How to get value with mouse event drag
我的 UI 一个 QScrollArea
有一张图片,我想在点击图片时获得一些价值。
更明确地说,我需要改变图像的亮度,我将通过鼠标获取该值。我已经看到 MouseMoveEvent
但我不知道如何使用它。
如果我在单击和拖动时获得鼠标的位置,我可以提取一个值来改变图像的亮度,我知道。我只是不知道我将如何获得这个职位。
有人知道我该怎么做吗?
Ps.: 我的 QScrollArea
是在 Design
上创建的,所以我没有根据 QScrollArea
的规范编写任何代码。
您需要的所有信息都在发送到小部件的 mouseMoveEvent
处理程序的 QMouseEvent
对象中。
QMouseEvent::buttons()
QMouseEvent::pos()
一种简单的方法是在收到 "mouse movement event" 并且 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.
// ...
}
}
}
有一个小的更正。当我像这样编程时,我的动作有点生涩。所以我尝试修改代码,现在工作完美。
这是我的改变
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();
}
我的 UI 一个 QScrollArea
有一张图片,我想在点击图片时获得一些价值。
更明确地说,我需要改变图像的亮度,我将通过鼠标获取该值。我已经看到 MouseMoveEvent
但我不知道如何使用它。
如果我在单击和拖动时获得鼠标的位置,我可以提取一个值来改变图像的亮度,我知道。我只是不知道我将如何获得这个职位。
有人知道我该怎么做吗?
Ps.: 我的 QScrollArea
是在 Design
上创建的,所以我没有根据 QScrollArea
的规范编写任何代码。
您需要的所有信息都在发送到小部件的 mouseMoveEvent
处理程序的 QMouseEvent
对象中。
QMouseEvent::buttons()
QMouseEvent::pos()
一种简单的方法是在收到 "mouse movement event" 并且 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.
// ...
}
}
}
有一个小的更正。当我像这样编程时,我的动作有点生涩。所以我尝试修改代码,现在工作完美。 这是我的改变
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();
}