ContextMenu QML Type

ContextMenu 첨부 유형은 플랫폼에 적합한 방식으로 컨텍스트 메뉴를 여는 방법을 제공합니다. 더 보기...

Import Statement: import QtQuick.Controls
Since: Qt 6.9

속성

신호

상세 설명

오른쪽 클릭 또는 컨텍스트 메뉴 키와 같은 플랫폼별 이벤트가 발생하면 컨텍스트 메뉴를 표시하려면 item 에 ContextMenu를 첨부할 수 있습니다.

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 을 공유할 수 있습니다. 이렇게 하면 컨텍스트 메뉴가 필요한 항목에 공통된 데이터가 있는 경우 단일 메뉴를 재사용할 수 있습니다. 예를 들어

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"
        }
    }
}

성능

컨텍스트 메뉴는 요청이 있을 때만 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가 도입되기 전에 작성된 레거시 애플리케이션은 예상대로 계속 작동할 수 있습니다.

속성 문서

이 속성은 열릴 컨텍스트 메뉴를 저장합니다. 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.