Skip to content

Commit

Permalink
Widgets: ignore unhandled right mouse button presses
Browse files Browse the repository at this point in the history
In many places this was already done; but there was some old code
remaining where mousePressEvent() simply returned without ignoring the
unhandled event.

Task-number: QTBUG-93486
Task-number: QTBUG-132066
Pick-to: 6.9
Change-Id: I4a876980b7ef88ee478fa8cfd9f68b5be5b217a2
Reviewed-by: Tor Arne Vestbø <[email protected]>
  • Loading branch information
ec1oud committed Dec 11, 2024
1 parent 10c844f commit a117525
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/widgets/widgets/qabstractspinbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,7 @@ void QAbstractSpinBox::mousePressEvent(QMouseEvent *event)

d->keyboardModifiers = event->modifiers();
if (event->button() != Qt::LeftButton || d->buttonState != None) {
event->ignore();
return;
}

Expand Down
4 changes: 3 additions & 1 deletion src/widgets/widgets/qlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1503,8 +1503,10 @@ void QLineEdit::mousePressEvent(QMouseEvent* e)

if (d->sendMouseEventToInputContext(e))
return;
if (e->button() == Qt::RightButton)
if (e->button() == Qt::RightButton) {
e->ignore();
return;
}
#ifdef QT_KEYPAD_NAVIGATION
if (QApplication::QApplicationPrivate() && !hasEditFocus()) {
setEditFocus(true);
Expand Down
4 changes: 3 additions & 1 deletion src/widgets/widgets/qscrollbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,10 @@ void QScrollBar::mousePressEvent(QMouseEvent *e)

if (d->maximum == d->minimum // no range
|| (e->buttons() & (~e->button())) // another button was clicked before
|| !(e->button() == Qt::LeftButton || (midButtonAbsPos && e->button() == Qt::MiddleButton)))
|| !(e->button() == Qt::LeftButton || (midButtonAbsPos && e->button() == Qt::MiddleButton))) {
e->ignore();
return;
}

d->pressedControl = style()->hitTestComplexControl(QStyle::CC_ScrollBar, &opt, e->position().toPoint(), this);
d->pointerOutsidePressedControl = false;
Expand Down
15 changes: 15 additions & 0 deletions tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ private slots:
void taskQTBUG_4679_selectToStartEndOfBlock();
#ifndef QT_NO_CONTEXTMENU
void taskQTBUG_7902_contextMenuCrash();
void contextMenu();
#endif
void taskQTBUG_7395_readOnlyShortcut();
void QTBUG697_paletteCurrentColorGroup();
Expand Down Expand Up @@ -4025,6 +4026,20 @@ void tst_QLineEdit::taskQTBUG_7902_contextMenuCrash()
QTest::qWait(300);
// No crash, it's allright.
}

void tst_QLineEdit::contextMenu() // QTBUG-132066
{
QLineEdit le;
le.show();
QVERIFY(QTest::qWaitForWindowExposed(&le));

// right-click: QLineEdit::mousePressEvent() should ignore the mouse press;
// QLineEdit::contextMenuEvent() should then be called to create and open a context menu
QTest::mouseClick(le.windowHandle(), Qt::RightButton, {}, le.rect().center());
QTRY_VERIFY(le.findChild<QMenu *>());

// This test could be extended to check and activate menu items.
}
#endif

void tst_QLineEdit::taskQTBUG_7395_readOnlyShortcut()
Expand Down

0 comments on commit a117525

Please sign in to comment.