diff options
author | Shawn Rutledge <[email protected]> | 2025-03-19 19:55:46 +0100 |
---|---|---|
committer | Mitch Curtis <[email protected]> | 2025-04-08 12:37:00 +0800 |
commit | 64063946db1760ec33d5329148ee14a816e6db38 (patch) | |
tree | 02208dc1818ced013e8be9699a5768d71df4c5df | |
parent | 38d26e8d2c87e49e92a5c9d450a8dce23a2de1f0 (diff) |
TextEditor example: add actions to built-in context menu, remove custom menu
Since 3b598b6f7509f57e198e7de1f04e4333555e7227 TextArea comes with a built-in
context menu. Here we demonstrate how to add extra menu items to the end.
Pick-to: 6.9
Task-number: QTBUG-35598
Task-number: QTBUG-134903
Change-Id: I1e3d8c044939521aaa076486630e3b879130dfd1
Reviewed-by: Mitch Curtis <[email protected]>
-rw-r--r-- | examples/quickcontrols/texteditor/doc/src/qtquickcontrols-texteditor.qdoc | 21 | ||||
-rw-r--r-- | examples/quickcontrols/texteditor/qml/texteditor.qml | 69 |
2 files changed, 35 insertions, 55 deletions
diff --git a/examples/quickcontrols/texteditor/doc/src/qtquickcontrols-texteditor.qdoc b/examples/quickcontrols/texteditor/doc/src/qtquickcontrols-texteditor.qdoc index d0ecc015ed..50e6a002d9 100644 --- a/examples/quickcontrols/texteditor/doc/src/qtquickcontrols-texteditor.qdoc +++ b/examples/quickcontrols/texteditor/doc/src/qtquickcontrols-texteditor.qdoc @@ -106,23 +106,12 @@ arrow keys, or by typing a lot of text), \l TextArea scrolls the \l Flickable to keep the cursor visible. - There is a context menu; we use a TapHandler to detect a right-click and - open it: + We take the same actions that we declared in the \l MenuBar and \l ToolBar + and append them to the existing items in the standard + \l {ContextMenu}{context menu} provided by \c TextArea: - \skipto TapHandler - \printuntil } - - The context \l Menu contains \l {MenuItem}{MenuItems} that reuse the same - \l Action objects as the main \l MenuBar and \l ToolBar are using. - As before, it's enough to bind \l {AbstractButton::}{action} to the - reusable Action that represents the operation to be performed. However, - we override each menu item's \l {MenuItem::}{text} to omit the - underlined mnemonics on the context menu. - - \skipto Menu - \printuntil MenuItem - \printuntil } - \dots 8 + \skipto ContextMenu + \printto } We consistently use the \l [QML] {Qt::}{qsTr()} function to enable translation of UI text, so that the application will make sense regardless diff --git a/examples/quickcontrols/texteditor/qml/texteditor.qml b/examples/quickcontrols/texteditor/qml/texteditor.qml index 154a531752..0ef3937468 100644 --- a/examples/quickcontrols/texteditor/qml/texteditor.qml +++ b/examples/quickcontrols/texteditor/qml/texteditor.qml @@ -152,6 +152,32 @@ ApplicationWindow { onTriggered: textArea.cursorSelection.alignment = Qt.AlignJustify } + Action { + id: fontDialogAction + text: qsTr("Fon&t…") + shortcut: "Ctrl+T" + onTriggered: { + fontDialog.selectedFont = textArea.cursorSelection.font + fontDialog.open() + } + } + + Action { + id: colorDialogAction + text: qsTr("Color…") + shortcut: "Ctrl+Shift+C" + onTriggered: { + colorDialog.selectedColor = textArea.cursorSelection.color + colorDialog.open() + } + } + + Component { + id: menuSeparatorComponent + + MenuSeparator {} + } + menuBar: MenuBar { Menu { title: qsTr("&File") @@ -465,11 +491,6 @@ ApplicationWindow { bottomPadding: 0 background: null - TapHandler { - acceptedButtons: Qt.RightButton - onTapped: contextMenu.popup() - } - onLinkActivated: function (link) { Qt.openUrlExternally(link) } @@ -479,6 +500,10 @@ ApplicationWindow { textDocument.source = "file:" + Qt.application.arguments[1] else textDocument.source = "qrc:/texteditor.html" + const menu = textArea.ContextMenu.menu + menu.addItem(menuSeparatorComponent.createObject(menu.contentItem)) + menu.addAction(fontDialogAction) + menu.addAction(colorDialogAction) } textDocument.onStatusChanged: { @@ -495,41 +520,7 @@ ApplicationWindow { errorDialog.open() } } - } - } - - Menu { - id: contextMenu - - MenuItem { - text: qsTr("Copy") - action: copyAction - } - MenuItem { - text: qsTr("Cut") - action: cutAction - } - MenuItem { - text: qsTr("Paste") - action: pasteAction - } - - MenuSeparator {} - - MenuItem { - text: qsTr("Font...") - onTriggered: function () { - fontDialog.selectedFont = textArea.cursorSelection.font - fontDialog.open() - } - } - MenuItem { - text: qsTr("Color...") - onTriggered: function () { - colorDialog.selectedColor = textArea.cursorSelection.color - colorDialog.open() - } } } |