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: { /* ... */ }
}
}
}コンテキストメニューの共有
1つのMenu を、複数のアタッチされたコンテキストメニューオブジェクト間で共有することが可能です。これにより、コンテキストメニューを必要とする項目に共通のデータがある場合、単一のMenuを再利用できます。例:
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を指定しないことを推奨します。IDを指定すると、この最適化が機能しなくなります。例:
Pane {
anchors.fill: parent
ContextMenu.menu: Menu {
// This prevents lazy creation of the Menu.
id: myMenu
// ...
}
}「Sharing context menus 」セクションの例が機能するのは、Menu が代入箇所とは別に定義されているためです。
他のメニューとの連携
例えばTapHandler やその他の手段を介してMenu が開かれた場合、ContextMenuは同時に開かれません。これにより、ContextMenuが導入される前に作成されたレガシーアプリケーションが、期待どおりに動作し続けることが可能になります。
ネイティブメニューのサポート
ContextMenuは、iOS上のネイティブメニューによってサポートされています。
プロパティのドキュメント
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()も参照してください 。
© 2026 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.