Fix QQuickItem's setAcceptedMouseButtons function

When using setAcceptedMouseButtons to only allow the LeftButton, the
user can click the LeftButton and while still holding it press the
RightButton. There would be a press event sent for both. To resolve this,
a check needed to be added to ensure the acceptedMouseButtons are
checked when a second press comes in.

[ChangeLog][QtQuick][QQuickItem] Fixed issue with mouse button events
being sent even when they were disabled by setAcceptedMouseButtons.

Change-Id: I064f3ff56ede12b1572e172be326eb337e280750
Task-number: QTBUG-31861
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@qt.io>
Reviewed-by: Robin Burchell <robin.burchell@viroteck.net>
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
Dan Cape 2015-10-07 15:46:14 -04:00 committed by Shawn Rutledge
parent c016634478
commit 0a87552e81
2 changed files with 29 additions and 0 deletions

View File

@ -1584,6 +1584,12 @@ bool QQuickWindowPrivate::deliverMouseEvent(QMouseEvent *event)
}
if (mouseGrabberItem) {
if (event->button() != Qt::NoButton
&& mouseGrabberItem->acceptedMouseButtons()
&& !(mouseGrabberItem->acceptedMouseButtons() & event->button())) {
event->ignore();
return false;
}
QPointF localPos = mouseGrabberItem->mapFromScene(event->windowPos());
QScopedPointer<QMouseEvent> me(cloneMouseEvent(event, &localPos));
me->accept();

View File

@ -172,6 +172,8 @@ private slots:
void contains_data();
void contains();
void ignoreButtonPressNotInAcceptedMouseButtons();
private:
enum PaintOrderOp {
@ -1971,6 +1973,27 @@ void tst_qquickitem::contains()
QCOMPARE(result.toBool(), contains);
}
void tst_qquickitem::ignoreButtonPressNotInAcceptedMouseButtons()
{
// Verify the fix for QTBUG-31861
TestItem item;
QCOMPARE(item.acceptedMouseButtons(), Qt::MouseButtons(Qt::NoButton));
QQuickWindow window;
item.setSize(QSizeF(200,100));
item.setParentItem(window.contentItem());
item.setAcceptedMouseButtons(Qt::LeftButton);
QCOMPARE(item.acceptedMouseButtons(), Qt::MouseButtons(Qt::LeftButton));
QTest::mousePress(&window, Qt::LeftButton, 0, QPoint(50, 50));
QTest::mousePress(&window, Qt::RightButton, 0, QPoint(50, 50)); // ignored because it's not LeftButton
QTest::mouseRelease(&window, Qt::RightButton, 0, QPoint(50, 50)); // ignored because it didn't grab the RightButton press
QTest::mouseRelease(&window, Qt::LeftButton, 0, QPoint(50, 50));
QCOMPARE(item.pressCount, 1);
QCOMPARE(item.releaseCount, 1);
}
QTEST_MAIN(tst_qquickitem)