2023-11-29 05:29:25 +00:00
|
|
|
// Copyright (C) 2023 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
|
|
|
|
|
|
|
import QtCore
|
|
|
|
import QtQuick
|
|
|
|
import QtQuick.Layouts
|
|
|
|
import QtQuick.Controls
|
|
|
|
|
|
|
|
ApplicationWindow {
|
|
|
|
id: window
|
|
|
|
width: 800
|
|
|
|
height: 600
|
|
|
|
visible: true
|
|
|
|
title: qsTr("Menus - style: %1").arg(currentStyle)
|
|
|
|
|
|
|
|
required property string currentStyle
|
|
|
|
|
|
|
|
Shortcut {
|
|
|
|
sequence: "Ctrl+Q"
|
|
|
|
onActivated: Qt.quit()
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings {
|
|
|
|
id: settings
|
|
|
|
|
|
|
|
property alias windowX: window.x
|
|
|
|
property alias windowY: window.y
|
|
|
|
property alias windowWidth: window.width
|
|
|
|
property alias windowHeight: window.height
|
|
|
|
}
|
|
|
|
|
|
|
|
menuBar: MenuBar {
|
2024-01-30 02:22:23 +00:00
|
|
|
requestNative: requestNativeAction.checked
|
|
|
|
|
2023-11-29 05:29:25 +00:00
|
|
|
Menu {
|
|
|
|
title: qsTr("Edit")
|
2024-02-06 06:20:10 +00:00
|
|
|
objectName: title
|
2023-11-29 05:29:25 +00:00
|
|
|
|
|
|
|
Action {
|
|
|
|
text: qsTr("Cut")
|
|
|
|
enabled: textArea.selectedText.length > 0
|
|
|
|
}
|
|
|
|
Action {
|
|
|
|
text: qsTr("Copy")
|
|
|
|
enabled: textArea.selectedText.length > 0
|
|
|
|
}
|
|
|
|
Action {
|
|
|
|
text: qsTr("Paste")
|
|
|
|
enabled: textArea.activeFocus
|
|
|
|
}
|
2024-01-30 02:22:23 +00:00
|
|
|
}
|
2023-11-29 05:29:25 +00:00
|
|
|
|
2024-01-30 02:22:23 +00:00
|
|
|
Menu {
|
|
|
|
title: qsTr("Options")
|
2024-02-06 06:20:10 +00:00
|
|
|
objectName: title
|
2023-11-29 05:29:25 +00:00
|
|
|
|
2024-01-30 02:22:23 +00:00
|
|
|
Action {
|
|
|
|
id: requestNativeAction
|
|
|
|
text: qsTr("Request native")
|
|
|
|
checkable: true
|
|
|
|
checked: true
|
|
|
|
}
|
2023-11-29 05:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnLayout {
|
|
|
|
anchors.fill: parent
|
|
|
|
|
|
|
|
Label {
|
|
|
|
text: qsTr("Right click on the window background to open a context menu. "
|
|
|
|
+ "Right click on the TextArea to access its edit context menu.\n\n"
|
|
|
|
+ "Things to check:\n\n"
|
2024-01-04 07:28:20 +00:00
|
|
|
+ "- Do the menu items trigger their actions (check console for output)?\n"
|
2024-01-17 07:06:20 +00:00
|
|
|
+ "- Do checkable menu items work?\n"
|
2023-11-29 05:29:25 +00:00
|
|
|
+ "- Do the Edit menu items (in the MenuBar menu and edit context menu)"
|
|
|
|
+ " work as expected with the TextArea?\n"
|
|
|
|
+ " - Are they enabled/disabled as expected?\n"
|
|
|
|
+ " - Does the TextArea keep focus after interacting with the Edit menu items?\n"
|
|
|
|
+ "- Does adding and removing menu items work?")
|
|
|
|
verticalAlignment: Text.AlignVCenter
|
|
|
|
wrapMode: Text.Wrap
|
|
|
|
|
|
|
|
Layout.alignment: Qt.AlignHCenter
|
|
|
|
Layout.preferredWidth: window.width * 0.5
|
|
|
|
Layout.fillHeight: true
|
|
|
|
}
|
|
|
|
|
2024-01-04 07:28:20 +00:00
|
|
|
GroupBox {
|
|
|
|
title: qsTr("Context menu")
|
|
|
|
|
|
|
|
Layout.fillWidth: true
|
|
|
|
|
|
|
|
RowLayout {
|
|
|
|
anchors.fill: parent
|
|
|
|
|
|
|
|
Button {
|
|
|
|
text: qsTr("Add action")
|
|
|
|
onClicked: backgroundContextMenu.appendAction()
|
|
|
|
}
|
|
|
|
Button {
|
|
|
|
text: qsTr("Remove action")
|
|
|
|
onClicked: backgroundContextMenu.removeLastAction()
|
|
|
|
}
|
|
|
|
|
|
|
|
Button {
|
|
|
|
text: qsTr("Add sub-menu action")
|
|
|
|
onClicked: subMenu.appendAction()
|
|
|
|
}
|
|
|
|
Button {
|
|
|
|
text: qsTr("Remove sub-menu action")
|
|
|
|
onClicked: subMenu.removeLastAction()
|
|
|
|
}
|
|
|
|
|
|
|
|
Item {
|
|
|
|
Layout.fillWidth: true
|
|
|
|
}
|
2023-11-29 05:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TextArea {
|
|
|
|
id: textArea
|
|
|
|
text: qsTr("Dummy TextArea to test disabled menu items")
|
|
|
|
|
|
|
|
Layout.fillWidth: true
|
|
|
|
Layout.minimumHeight: 100
|
|
|
|
|
|
|
|
TapHandler {
|
|
|
|
objectName: "textAreaTapHandler"
|
|
|
|
acceptedButtons: Qt.RightButton
|
2024-01-30 02:22:23 +00:00
|
|
|
onTapped: editContextMenu.popup()
|
2023-11-29 05:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TapHandler {
|
|
|
|
objectName: "backgroundTapHandler"
|
|
|
|
acceptedButtons: Qt.RightButton
|
2024-01-30 02:22:23 +00:00
|
|
|
onTapped: backgroundContextMenu.popup()
|
2023-11-29 05:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Component {
|
|
|
|
id: actionComponent
|
|
|
|
|
|
|
|
Action {}
|
|
|
|
}
|
|
|
|
|
2024-01-04 07:28:20 +00:00
|
|
|
component ContextAction: Action {
|
2024-01-17 07:06:20 +00:00
|
|
|
id: action
|
|
|
|
onCheckedChanged: print("checked of \"" + text + "\" changed to " + action.checked)
|
|
|
|
onTriggered: print("triggered \"" + text + "\"")
|
2024-01-04 07:28:20 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 05:29:25 +00:00
|
|
|
Menu {
|
|
|
|
id: backgroundContextMenu
|
2024-02-06 06:20:10 +00:00
|
|
|
objectName: "backgroundContextMenu"
|
2024-01-30 02:22:23 +00:00
|
|
|
requestNative: requestNativeAction.checked
|
2023-11-29 05:29:25 +00:00
|
|
|
|
|
|
|
function appendAction() {
|
|
|
|
let action = actionComponent.createObject(null, { text: qsTr("Extra context menu item") })
|
|
|
|
backgroundContextMenu.addAction(action)
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeLastAction() {
|
|
|
|
// TODO: Can't use count here because it's 0: it uses contentModel->count(), but native menu items
|
|
|
|
// are not Qt Quick items, so we either need to document that you should use contentData.count
|
|
|
|
// or add an "actions" property. The problem with contentData is that it could contain
|
|
|
|
// non-Action objects. Another potential issue is that "It is not re-ordered when items are inserted or moved",
|
|
|
|
// making it unreliable as a general purpose container of actions if users add or remove them dynamically.
|
|
|
|
backgroundContextMenu.removeAction(backgroundContextMenu.actionAt(backgroundContextMenu.contentData.length - 1))
|
|
|
|
}
|
|
|
|
|
2024-01-04 07:28:20 +00:00
|
|
|
ContextAction {
|
2024-01-17 07:06:20 +00:00
|
|
|
text: qsTr("Context menu item")
|
2023-11-29 05:29:25 +00:00
|
|
|
}
|
2024-01-04 07:28:20 +00:00
|
|
|
ContextAction {
|
2024-01-17 07:06:20 +00:00
|
|
|
text: qsTr("Checkable context menu item")
|
|
|
|
checkable: true
|
2023-11-29 05:29:25 +00:00
|
|
|
}
|
2024-01-04 07:28:20 +00:00
|
|
|
ContextAction {
|
2024-01-17 07:06:20 +00:00
|
|
|
text: qsTr("Checked context menu item")
|
|
|
|
checkable: true
|
|
|
|
checked: true
|
2023-11-29 05:29:25 +00:00
|
|
|
}
|
2024-01-17 07:44:00 +00:00
|
|
|
ContextAction {
|
|
|
|
text: qsTr("Disabled context menu item")
|
|
|
|
enabled: false
|
|
|
|
}
|
|
|
|
ContextAction {
|
|
|
|
text: qsTr("Checked and disabled context menu item")
|
|
|
|
checkable: true
|
|
|
|
checked: true
|
|
|
|
enabled: false
|
|
|
|
}
|
2024-01-04 07:28:20 +00:00
|
|
|
|
2024-01-19 07:01:20 +00:00
|
|
|
MenuSeparator {}
|
2024-01-04 07:28:20 +00:00
|
|
|
|
|
|
|
Menu {
|
|
|
|
id: subMenu
|
|
|
|
title: qsTr("Sub-menu")
|
2024-02-06 06:20:10 +00:00
|
|
|
objectName: title
|
2024-01-04 07:28:20 +00:00
|
|
|
|
|
|
|
function appendAction() {
|
|
|
|
let action = actionComponent.createObject(null, { text: qsTr("Extra sub-menu item") })
|
|
|
|
subMenu.addAction(action)
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeLastAction() {
|
|
|
|
subMenu.removeAction(subMenu.actionAt(subMenu.contentData.length - 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
ContextAction {
|
2024-01-17 07:06:20 +00:00
|
|
|
text: qsTr("Sub-menu item")
|
2024-01-04 07:28:20 +00:00
|
|
|
}
|
|
|
|
ContextAction {
|
2024-01-17 07:06:20 +00:00
|
|
|
text: qsTr("Checkable sub-menu item")
|
|
|
|
checkable: true
|
2024-01-04 07:28:20 +00:00
|
|
|
}
|
|
|
|
ContextAction {
|
2024-01-17 07:06:20 +00:00
|
|
|
text: qsTr("Checked sub-menu item")
|
|
|
|
checkable: true
|
|
|
|
checked: true
|
2024-01-04 07:28:20 +00:00
|
|
|
}
|
2024-01-19 07:01:20 +00:00
|
|
|
|
|
|
|
MenuSeparator {}
|
|
|
|
|
2024-01-17 07:44:00 +00:00
|
|
|
ContextAction {
|
|
|
|
text: qsTr("Disabled sub-menu item")
|
|
|
|
enabled: false
|
|
|
|
}
|
|
|
|
ContextAction {
|
|
|
|
text: qsTr("Checked and disabled sub-menu item")
|
|
|
|
checkable: true
|
|
|
|
checked: true
|
|
|
|
enabled: false
|
|
|
|
}
|
2024-01-04 07:28:20 +00:00
|
|
|
}
|
2023-11-29 05:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Menu {
|
|
|
|
id: editContextMenu
|
2024-02-06 06:20:10 +00:00
|
|
|
objectName: "editContextMenu"
|
2024-01-30 02:22:23 +00:00
|
|
|
requestNative: requestNativeAction.checked
|
2023-11-29 05:29:25 +00:00
|
|
|
|
2024-01-04 07:28:20 +00:00
|
|
|
ContextAction {
|
2023-11-29 05:29:25 +00:00
|
|
|
text: qsTr("Cut")
|
|
|
|
enabled: textArea.selectedText.length > 0
|
|
|
|
}
|
2024-01-04 07:28:20 +00:00
|
|
|
ContextAction {
|
2023-11-29 05:29:25 +00:00
|
|
|
text: qsTr("Copy")
|
|
|
|
enabled: textArea.selectedText.length > 0
|
|
|
|
}
|
2024-01-04 07:28:20 +00:00
|
|
|
ContextAction {
|
2023-11-29 05:29:25 +00:00
|
|
|
text: qsTr("Paste")
|
|
|
|
enabled: textArea.activeFocus
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|