{"id":2757,"date":"2025-11-20T11:50:04","date_gmt":"2025-11-20T02:50:04","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=2757"},"modified":"2025-11-21T09:43:42","modified_gmt":"2025-11-21T00:43:42","slug":"qtmenu","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2025\/11\/20\/qtmenu\/","title":{"rendered":"[QT] Menu &#038; Dynamic Menu"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb QML\uc5d0\uc11c \uba54\ub274\ub97c \ub9cc\ub4dc\ub294 \ubc29\ubc95\uc744 \uc124\uba85\ud569\ub2c8\ub2e4.<br>Explains how to create a menu in QML.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc124\uba85\uc740 \uc8fc\uc11d\uc744 \ucc38\uace0 \ud558\uc138\uc694<br>Please refer to the comments for explanation.<\/p>\n\n\n\n<p>\u2714\ufe0fMain.cpp<\/p>\n\n\n\n<p>&#8212; Main.cpp\ud30c\uc77c\uc740 \uc218\uc815\ud558\uc9c0 \uc54a\uc544\ub3c4 \ub429\ub2c8\ub2e4.<br>You do not need to modify the Main.cpp file.<\/p>\n\n\n\n<p>\u2714\ufe0fMain.qml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import QtQuick\nimport QtQuick.Controls\n\n\/\/ QML\uc5d0\uc11c ApplicationWindow \uc0ac\uc6a9 \ucd94\ucc9c\n\/\/ Recommended to use ApplicationWindow in QML\nApplicationWindow {\n    width: 400\n    height: 300\n    visible: true\n    title: \"Menu Example\"\n\n    \/\/ == \ucef4\ud3ec\ub10c\ud2b8 \uba54\ub274 \/ Component Menu ==\n    menuBar: AppMenuBar {}\n\n    \/\/ \ud14d\uc2a4\ud2b8 \uc804\uccb4 \uc815\ub82c \/ Align entire text\n    Text {\n        text: \"Center Text\"\n        anchors.centerIn: parent\n    }\n\n    \/\/ \uc218\ud3c9\ub9cc \uc911\uc559 \uc815\ub82c \/ Center alignment only horizontally\n    Text {\n        text: \"horizontalCenter\"\n        anchors.horizontalCenter: parent.horizontalCenter\n        \/\/ anchors.left\uac00 \uc788\uc5b4\uc57c leftMargin\uc774 \uc801\uc6a9\ub428.\n        anchors.top: parent.top\n        anchors.topMargin: 50\n\n    }\n\n    \/\/ \uc218\uc9c1 \uc911\uc559 \uc815\ub82c \/ vertical center alignment\n    Text {\n        text: \"Vertical Center\"\n        anchors.verticalCenter: parent.verticalCenter\n        \/\/ anchors.left\uac00 \uc788\uc5b4\uc57c leftMargin\uc774 \uc801\uc6a9\ub428.\n        anchors.left: parent.left\n        anchors.leftMargin: 30\n    }\n}\n\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f NewWindow.qml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import QtQuick\nimport QtQuick.Controls\n\n\/*-----------------------------------------------------------------------------------------------\n\n    -- \ub3d9\uc801 \uba54\ub274  \/ Dynamic Menu--\n    \uc774 \ucc3d \ub0b4\uc5d0\uc11c \ucef4\ud3ec\ub10c\ud2b8\ub97c \uc815\uc758\ud558\uace0 \ubc84\ud2bc \ud074\ub9ad\uc2dc \uba54\ub274\ub97c \uad50\uccb4\ud558\ub294 \uba54\ub274\n    A menu that defines components within this window and replaces them when a button is clicked.\n\n-------------------------------------------------------------------------------------------------*\/\n\nApplicationWindow {\n    id: root\n    width: 400\n    height: 300\n    visible: true\n    title: \"Dynamic Menu Example\"\n\n    property bool adminMode: false\n\n    \/\/  \uba54\ub274\ub97c \uc2e4\uc81c\ub85c \ubcf4\uc5ec\uc8fc\ub294 \uacf3 \/ Where the menu actually appears\n    menuBar: Loader {\n        id: menuLoader\n        sourceComponent: adminMode ? adminMenu : normalMenu\n    }\n\n    \/\/ \uc77c\ubc18 \uc0ac\uc6a9\uc790 \uba54\ub274 \/ General user menu\n    Component {\n        id: normalMenu\n        MenuBar {\n            Menu {\n                title: \"File\"\n                MenuItem {\n                    text: \"Open\"\n                    onTriggered: console.log(\"Open clicked (normal user)\")\n                }\n                MenuItem {\n                    text: \"Exit\"\n                    onTriggered: Qt.quit()\n                }\n            }\n            Menu {\n                title: \"Help\"\n                MenuItem {\n                    text: \"About\"\n                    onTriggered: console.log(\"About clicked (normal user)\")\n                }\n            }\n        }\n    }\n\n    \/\/  \uad00\ub9ac\uc790 \uba54\ub274 \/ Administrator Menu\n    Component {\n        id: adminMenu\n        MenuBar {\n            Menu {\n                title: \"Admin\"\n                MenuItem {\n                    text: \"User Management\"\n                    onTriggered: console.log(\"User management clicked (admin)\")\n                }\n                MenuItem {\n                    text: \"System Log\"\n                    onTriggered: console.log(\"System log clicked (admin)\")\n                }\n            }\n            Menu {\n                title: \"Tools\"\n                MenuItem {\n                    text: \"Diagnostics\"\n                    onTriggered: console.log(\"Diagnostics clicked (admin)\")\n                }\n            }\n        }\n    }\n\n    \/\/  \ubc84\ud2bc\uc73c\ub85c \uba54\ub274 \uad50\uccb4 \/ Change menu with buttons\n    Button {\n        text: adminMode ? \"Switch to Normal Menu\" : \"Switch to Admin Menu\"\n        anchors.centerIn: parent\n        onClicked: adminMode = !adminMode\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f AppMenuBar.qml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import QtQuick\nimport QtQuick.Controls\n\n\/\/ == \uc774 AppMenuBar\ud30c\uc77c\uc774 \ud558\ub098\uc758 \ucef4\ud3ec\ub10c\ud2b8\uac00 \ub428 \/ This AppMenuBar file becomes a component ==\n\n    MenuBar {\n        Menu {\n            title: qsTr(\"File\")\n            MenuItem {\n                text: qsTr(\"Open\")\n                onTriggered: console.log(\"Open clicked\")\n            }\n            MenuItem {\n                text: qsTr(\"Exit\")\n                onTriggered: Qt.quit()\n            }\n        }\n        Menu {\n            title: qsTr(\"Edit\")\n            MenuItem {\n                text: qsTr(\"Write\")\n                onTriggered: console.log(\"Write clicked\")\n            }\n            MenuItem {\n                text: qsTr(\"Action\")\n                onTriggered: Qt.quit()\n            }\n        }\n\n        Menu {\n            title: qsTr(\"View\")\n            MenuItem {\n                text: qsTr(\"Show left sidebar\")\n                onTriggered: console.log(\"About clicked\")\n            }\n            MenuItem {\n                text: qsTr(\"Show right sidebar\")\n                onTriggered: console.log(\"About clicked\")\n            }\n        }\n        Menu {\n            title: qsTr(\"Window\")\n            MenuItem {\n                text: qsTr(\"Open New Window\")\n                onTriggered: {\n                    console.log(\"Open New Window clicked\")\n\n                    \/\/ \uc0c8 \ucc3d \ucef4\ud3ec\ub10c\ud2b8 \uc0dd\uc131, \uba54\ub274\uc120\ud0dd\uc2dc \uc0c8\ucc3d \uc624\ud508\n                    \/\/ Create a new window component, open a new window when a menu is selected\n                    const comp = Qt.createComponent(\"NewWindow.qml\")\n                    if (comp.status === Component.Ready) {\n                        const win = comp.createObject(null)\n                        if (win) win.show()\n                    } else {\n                        console.error(\"Component load error:\", comp.errorString())\n                    }\n                }\n            }\n            MenuItem {\n                text: qsTr(\"Show right sidebar\")\n                onTriggered: console.log(\"About clicked\")\n            }\n        }\n        \/\/ \ub9e5os\uc5d0\uc11c\ub294 help\ub098 \uc774\ub984\uc774 \uc774\uc0c1\ud558\uba74 \uba54\ub274\uac00 \uc548 \ub098\ud0c0\ub0a0 \uc218 \uc788\uc74c\n        \/\/ In macOS, the menu may not appear if the help or name is strange.\n        Menu {\n            title: qsTr(\"help center\")\n            MenuItem {\n                text: qsTr(\"Record list\")\n                onTriggered: console.log(\"About clicked\")\n            }\n            MenuItem {\n                text: qsTr(\"Record help\")\n                onTriggered: console.log(\"About clicked\")\n            }\n        }\n    }\n\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f CMakeList.txt<\/p>\n\n\n\n<p>&#8212; qml\ud30c\uc77c\uc774 \uc544\ub798\uc758 \ucf54\ub4dc\ucc98\ub7fc \ub4f1\ub85d\ub418\uc5b4 \uc788\uc5b4\uc57c \ud569\ub2c8\ub2e4.<br>The qml file must be registered as shown in the code below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>qt_add_qml_module(appMenu\n    URI Menu\n    VERSION 1.0\n    QML_FILES\n        Main.qml\n        AppMenuBar.qml\n        NewWindow.qml\n)<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uc2a4\ud06c\ub9b0\uc0f7 \/ ScreenShot<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"[QT,QML] -Menu &amp; Dynamic Menu\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/3Ujj3_WSXe4?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb QML\uc5d0\uc11c \uba54\ub274\ub97c \ub9cc\ub4dc\ub294 \ubc29\ubc95\uc744 \uc124\uba85\ud569\ub2c8\ub2e4.Explains how to create a menu in QML. \ud83d\udc49\ud83c\udffb \uc124\uba85\uc740 \uc8fc\uc11d\uc744 \ucc38\uace0 \ud558\uc138\uc694Please refer to the comments for explanation. \u2714\ufe0fMain.cpp &#8212; Main.cpp\ud30c\uc77c\uc740 \uc218\uc815\ud558\uc9c0 \uc54a\uc544\ub3c4 \ub429\ub2c8\ub2e4.You do not need to modify the Main.cpp file. \u2714\ufe0fMain.qml \u2714\ufe0f NewWindow.qml \u2714\ufe0f AppMenuBar.qml \u2714\ufe0f CMakeList.txt &#8212; qml\ud30c\uc77c\uc774 \uc544\ub798\uc758 \ucf54\ub4dc\ucc98\ub7fc \ub4f1\ub85d\ub418\uc5b4 \uc788\uc5b4\uc57c \ud569\ub2c8\ub2e4.The qml file must [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,1],"tags":[],"class_list":["post-2757","post","type-post","status-publish","format-standard","hentry","category-qt","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=2757"}],"version-history":[{"count":8,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2757\/revisions"}],"predecessor-version":[{"id":2771,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2757\/revisions\/2771"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=2757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=2757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=2757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}