ContextMenu QML Type
ContextMenuアタッチ型は、プラットフォームに適した方法でコンテキストメニューを開く方法を提供します。詳細...
Import Statement: | import QtQuick.Controls |
Since: | Qt 6.9 |
プロパティ
- menu : Menu
信号
- requested(point position)
詳しい説明
ContextMenu は、右クリックやコンテキスト メニュー キーなどのプラットフォーム固有のイベントに応じてコンテキスト メニューを表示するために、任意のitem にアタッチできます。
Pane { anchors.fill: parent ContextMenu.menu: Menu { MenuItem { text: qsTr("Eat Tomato") onTriggered: { /* ... */ } } MenuItem { text: qsTr("Throw Tomato") onTriggered: { /* ... */ } } MenuItem { text: qsTr("Squash Tomato") onTriggered: { /* ... */ } } } }
コンテキストメニューの共有
アタッチされた複数のコンテキストメニューオブジェクト間でMenu を共有することができます。これにより、コンテキストメニューが必要なアイテムに共通のデータがある場合、1つのメニューを再利用することができます。例えば
pragma ComponentBehavior: Bound import QtQuick import QtQuick.Controls.Basic import QtQuick.Templates as T ApplicationWindow { width: 600 height: 400 visible: true component Tomato: Label { id: tomato objectName: text horizontalAlignment: Label.AlignHCenter verticalAlignment: Label.AlignVCenter width: Math.max(200, contentWidth * 1.5, contentWidth * 1.5) height: width color: skinColor function eat() { print("Ate " + text) } function ditch() { print("Threw " + text) } function squash() { print("Squashed " + text) } property color skinColor: "tomato" background: Rectangle { color: tomato.skinColor radius: width / 2 } ContextMenu.menu: contextMenu } Menu { id: contextMenu readonly property Tomato triggerItem: parent as Tomato readonly property string triggerItemText: triggerItem?.text ?? "" MenuItem { text: qsTr("Eat %1").arg(contextMenu.triggerItemText) onTriggered: contextMenu.triggerItem.eat() } MenuItem { text: qsTr("Throw %1").arg(contextMenu.triggerItemText) onTriggered: contextMenu.triggerItem.ditch() } MenuItem { text: qsTr("Squash %1").arg(contextMenu.triggerItemText) onTriggered: contextMenu.triggerItem.squash() } } Row { anchors.centerIn: parent Tomato { text: qsTr("tomato") } Tomato { text: qsTr("really ripe tomato") skinColor: "maroon" } } }
パフォーマンス
ContextMenu は、要求されたときだけMenu
を作成します。この最適化がなければ、Menu
は、通常アプリケーションの起動時である、含むコンポーネントがロードされるときに作成されるでしょう。
ContextMenuのmenu プロパティに割り当てられたMenu
は、それが割り当てられた場所で定義されているときは、idを与えないことをお勧めします。そうすることで、この最適化を防ぐことができます。例えば
Pane { anchors.fill: parent ContextMenu.menu: Menu { // This prevents lazy creation of the Menu. id: myMenu // ... } }
Sharing context menus セクションの例は、Menu
がその割り当てとは別に定義されているため、動作します。
他のメニューとの相互作用
Menu
がTapHandler などを経由して開かれた場合、ContextMenu は同時に開きません。これにより、ContextMenuが導入される前に作成されたレガシーアプリケーションは、期待どおりに動作し続けることができます。
プロパティ Documentation
menu : Menu |
このプロパティは、開かれるコンテキストメニューを保持する。Menu オブジェクトに設定することができます。
注意: このプロパティに割り当てられたMenu に id を指定することはできません。詳しくはSharing context menus を参照。
シグナル・ドキュメント
requested(point position) |
このシグナルは、コンテキストメニューが要求されたときに発せられる。
マウスの右ボタンクリックによって要求された場合、position 、親からの相対的なクリック位置が示されます。
以下の例では、プログラムでコンテキストメニューを開く方法を示しています:
Button { id: button text: qsTr("Click me!") ContextMenu.onRequested: position => { const menu = buttonMenu.createObject(button) menu.popup(position) } } Component { id: buttonMenu Menu { MenuItem { text: qsTr("Open") } } }
メニューが設定されていなくても、このシグナルが接続されていれば、コンテキストメニューイベントは受け付けられ、伝播しません。
注: 対応するハンドラはonRequested
。
QContextMenuEvent::pos()も参照してください 。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.