summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2023-05-24 12:20:12 +0200
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2023-05-25 14:06:57 +0200
commit6a2b0291389fa32bd70a2c03119890275d5e0681 (patch)
treeb5289b62038dec97e63d9e7c90a26e0451019b96
parent16cf095bd5018d8fd935233c3fa54eb85c685ea3 (diff)
QLabel: always show the context menu created by the control
Amends e718818745e6db8df09ea55c4071e116f00851c9, after which a context menu was only shown if the format was rich text and there was a link at the position of the click. We always want to leave it up to the control to create a context menu, so only return early if there is no control. The control will then respect content at the position (i.e. link or not) and text interaction flags, and create the menu based on that. I.e. a rich text label with selectable text should still show "Select All" in the context menu, also if not clicking on a link. Add a test case to verify that the context menu event got accepted as expected, which indicates that the label showed a menu. Pick-to: 6.5 Change-Id: Ib2b36286e4f1253d10489b5add83e8cdd7197a06 Reviewed-by: Oliver Eftevaag <oliver.eftevaag@qt.io>
-rw-r--r--src/widgets/widgets/qlabel.cpp7
-rw-r--r--tests/auto/widgets/widgets/qlabel/tst_qlabel.cpp39
2 files changed, 40 insertions, 6 deletions
diff --git a/src/widgets/widgets/qlabel.cpp b/src/widgets/widgets/qlabel.cpp
index 4d1c4ebb74..bc40b0f442 100644
--- a/src/widgets/widgets/qlabel.cpp
+++ b/src/widgets/widgets/qlabel.cpp
@@ -1652,15 +1652,10 @@ QPoint QLabelPrivate::layoutPoint(const QPoint& p) const
#ifndef QT_NO_CONTEXTMENU
QMenu *QLabelPrivate::createStandardContextMenu(const QPoint &pos)
{
- if (!control || effectiveTextFormat == Qt::PlainText)
+ if (!control)
return nullptr;
const QPoint p = layoutPoint(pos);
- QString linkToCopy = control->document()->documentLayout()->anchorAt(p);
-
- if (linkToCopy.isEmpty())
- return nullptr;
-
return control->createStandardContextMenu(p, q_func());
}
#endif
diff --git a/tests/auto/widgets/widgets/qlabel/tst_qlabel.cpp b/tests/auto/widgets/widgets/qlabel/tst_qlabel.cpp
index 2775e2c683..9193a34c45 100644
--- a/tests/auto/widgets/widgets/qlabel/tst_qlabel.cpp
+++ b/tests/auto/widgets/widgets/qlabel/tst_qlabel.cpp
@@ -77,6 +77,8 @@ private Q_SLOTS:
#ifndef QT_NO_CONTEXTMENU
void taskQTBUG_7902_contextMenuCrash();
+ void contextMenu_data();
+ void contextMenu();
#endif
void taskQTBUG_48157_dprPixmap();
@@ -561,6 +563,43 @@ void tst_QLabel::taskQTBUG_7902_contextMenuCrash()
QTest::qWait(350);
// No crash, it's allright.
}
+
+void tst_QLabel::contextMenu_data()
+{
+ QTest::addColumn<QString>("text");
+ QTest::addColumn<Qt::TextInteractionFlag>("interactionFlags");
+ QTest::addColumn<bool>("showsContextMenu");
+
+ QTest::addRow("Read-only") << "Plain Text"
+ << Qt::NoTextInteraction
+ << false;
+ QTest::addRow("Selectable") << "Plain Text"
+ << Qt::TextEditorInteraction
+ << true;
+ QTest::addRow("Link") << "<a href=\"nowhere\">Rich text with link</a>"
+ << Qt::TextBrowserInteraction
+ << true;
+ QTest::addRow("Rich text") << "<b>Rich text without link</b>"
+ << Qt::TextBrowserInteraction
+ << true;
+}
+
+void tst_QLabel::contextMenu()
+{
+ QFETCH(QString, text);
+ QFETCH(Qt::TextInteractionFlag, interactionFlags);
+ QFETCH(bool, showsContextMenu);
+
+ QLabel label(text);
+ label.setTextInteractionFlags(interactionFlags);
+ label.show();
+ QVERIFY(QTest::qWaitForWindowExposed(&label));
+
+ const QPoint menuPosition = label.rect().center();
+ QContextMenuEvent cme(QContextMenuEvent::Mouse, menuPosition, label.mapToGlobal(menuPosition));
+ QApplication::sendEvent(&label, &cme);
+ QCOMPARE(cme.isAccepted(), showsContextMenu);
+}
#endif
void tst_QLabel::taskQTBUG_48157_dprPixmap()