이 페이지에서

ContextMenu QML Type

ContextMenu 부착형(attached type)은 플랫폼에 적합한 방식으로 컨텍스트 메뉴를 열 수 있는 방법을 제공합니다. 더 보기...

Import Statement: import QtQuick.Controls
Since: Qt 6.9

속성

신호

상세 설명

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 `를 여러 개의 연결된 컨텍스트 메뉴 객체 간에 공유할 수 있습니다. 이를 통해 컨텍스트 메뉴가 필요한 항목들이 공통된 데이터를 가질 때 단일 메뉴를 재사용할 수 있습니다. 예를 들어:

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 를 할당하는경우 , 네이티브 메뉴 지원을 보장하려면 popupType 를 Popup.Native 로 설정해야 합니다.

속성 문서

이 속성은 열릴 컨텍스트 메뉴를 저장합니다. 이 속성은 임의의 ` Menu ` 객체로 설정할 수 있습니다.

참고: 이 속성에 할당된 Menu 에는 ID를 지정할 수 없습니다. 자세한 내용은 Sharing context menus 을 참조하십시오.

Signal 문서

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.