한국어
Qt
 

이 글은 QtQuick기반 응용프로그램 개발 시 Window 에 메뉴바(MenuBar)와 메뉴 아이템을 추가하는 방법을 설명한다.

 

ApplicationWindow.png

 

응용프로그램에 메뉴바를 구성하려면 ApplicationWindow 를 루트 아이템으로 생성해야한다. 다음의 간단한 예제를 보자.

 

main.qml

import QtQuick 2.0
import QtQuick.Controls 2.0
import QtQuick.Dialogs 1.2

ApplicationWindow {
    id: window
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    menuBar: MyMenuBar{
        id: menubar
        fileDialog: browse
    }

    FileDialog{
        id: browse
        nameFilters: [ "Image files (*.jpg *.png)", "All files (*)" ]
    }
}

 

main.qml에서 ApplicationWindow 는 Window 에서 파생된 형태의 아이템이다. Window에 추가적으로 다음과 같은 프로퍼티들이 추가된다.

contentItem : Item
footer : Item
header : Item
menuBar : Item

 

QML에서 창의 속성, 모양 및 레이아웃을 제어 할 수 있으며 menuBar프로퍼티에 MenuBar 유형의 아이템을 추가할 수 있다.

qtquickcontrols2-applicationwindow-wireframe.png

▲ApplicationWindow 레이아웃

 

MyMenuBar.qml

import QtQuick 2.0
import QtQuick.Controls 2.12

MenuBar{
    property var fileDialog: null
    Menu{
        id: file
        title: qsTr("File")
        
        Action {
            text: qsTr("&New...")
        }
        Action {
            text: qsTr("&Open...")
            shortcut: "Ctrl+N"
            onTriggered: { fileDialog.open() }
        }
        Action {
            text: qsTr("&Save")
        }
        Action {
            text: qsTr("Save &As...")
        }
        MenuSeparator { }
        Action {
            text: qsTr("E&xit...")
            onTriggered: Qt.quit()
        }
    }
    
    Menu{
        id: edit
        title: qsTr("Edit")
        Action {
            text: qsTr("&Copy...")
        }
        Action {
            text: qsTr("&Paste...")
        }
        Action {
            text: qsTr("&Delete...")
        }
    }
    
    Menu{
        id: help
        title: qsTr("Help")
        Action {
            text: qsTr("&About...")
        }
    }
}

 

MenuBar에는 Menu 아이템을 포함하고 있으며 Menu에는 세부 액션(Action)들을 추가할 수 있다. Action은 행동을 정의하는 것으로 사용자의 요청을 응용프로그램이 처리할 수 있도록 triggered 시그널을 제공한다. (GoF의 디자인 패턴중 Command 패턴과 같다.)

 

위의 예제에서 사용자가 "Open"을 요청하면 파일다이얼로그(FileDialog)을 열도록 되어있음을 알 수 있다.

번호 제목 글쓴이 날짜 조회 수
공지 Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 85679
118 최초의 Qt 6.0 스냅샷 제공 (First Qt 6.0 Snapshot Available) j2doll 2020.06.21 587
117 Qt MQTT 에 대해서 file makersweb 2020.06.02 954
116 Embedded Linux 에서 Qt 및 Graphics Stack file 운영자 2020.05.27 606
115 ShaderEffect QML Type을 이용한 버튼 클릭 효과 file makersweb 2020.05.22 1079
114 Qt기반의 서버와 클라이언트간 SOAP(Simple Object Access Protocol) file makersweb 2020.05.11 964
113 재진입(Reentrancy) 및 스레드 안전성(Thread-Safety) makersweb 2020.04.19 1203
112 Qt 5.15 및 Qt 6의 출시 일정 makersweb 2020.04.09 926
111 콘솔에서 사용자 입력받기 file makersweb 2020.03.22 51830
110 컨테이너 클래스 - QVector makersweb 2020.03.17 2812
109 Qt로 작성된 안드로이드 APP에 Splash Screen을 추가 file makersweb 2020.03.10 877
108 QLabel의 텍스트 색과 배경색을 변경 makersweb 2020.02.25 6567
107 라즈베리파이4에 대한 Qt 5.14.1 크로스컴파일 [1] file makersweb 2020.02.12 4443
106 QOpenGLWidget 을 투명하게 적용 file makersweb 2020.02.05 1021
105 2020년에 변경되는 Qt 오퍼 (Qt offering changes 2020) [2] j2doll 2020.01.31 715
104 Qt로 XML 파싱 : Qt 6에서 업데이트된 (Parsing XML with Qt: Updates for Qt 6) [1] j2doll 2020.01.16 975
103 Qt 멀티 스레드 프로그래밍 시 유의해야 할 몇 가지 makersweb 2020.01.13 4847
» ApplicationWindow 와 메뉴바(MenuBar)구성 file makersweb 2020.01.04 1490
101 QThread 소개 및 예제 makersweb 2019.12.25 19374
100 Qt의 오픈소스 라이센스 소개 file makersweb 2019.12.15 12510
99 Qt for MCU 1.0 릴리즈 makersweb 2019.12.10 747