Qt使用教程:使用Qt Quick UI表單(六)
創(chuàng)建菜單
向?qū)砑右粋€菜單欄到main.qml文件中,這其中包含了一個具有Open和Exit菜單的File菜單。保存菜單和Exit菜單項,然后添加具有標(biāo)準(zhǔn)菜單項的Edit和Help菜單。
該向?qū)?chuàng)建下面的代碼:
menuBar: MenuBar { Menu { title: qsTr("&File") MenuItem { text: qsTr("&Open") onTriggered: messageDialog.show(qsTr("Open action triggered")); } MenuItem { text: qsTr("E&xit") onTriggered: Qt.quit(); } } }
刪除Open菜單項并添加下面的代碼來創(chuàng)建新的菜單:
menuBar: MenuBar { Menu { title: qsTr("&File") MenuItem { text: qsTr("E&xit") onTriggered: Qt.quit(); } } Menu { title: qsTr("&Edit") MenuItem { action: cutAction } MenuItem { action: copyAction } MenuItem { action: pasteAction } } Menu { title: qsTr("&Help") MenuItem { text: qsTr("About...") onTriggered: aboutDialog.open() } } } Action { id: copyAction text: qsTr("&Copy") shortcut: StandardKey.Copy iconName: "edit-copy" enabled: (!!activeFocusItem && !!activeFocusItem["copy"]) onTriggered: activeFocusItem.copy() } Action { id: cutAction text: qsTr("Cu&t") shortcut: StandardKey.Cut iconName: "edit-cut" enabled: (!!activeFocusItem && !!activeFocusItem["cut"]) onTriggered: activeFocusItem.cut() } Action { id: pasteAction text: qsTr("&Paste") shortcut: StandardKey.Paste iconName: "edit-paste" enabled: (!!activeFocusItem && !!activeFocusItem["paste"]) onTriggered: activeFocusItem.paste() }
創(chuàng)建對話框
該向?qū)⒃趍ain.qml文件文件中創(chuàng)建一個消息對話框:
MessageDialog { id: messageDialog title: qsTr("May I have your attention, please?") function show(caption) { messageDialog.text = caption; messageDialog.open(); }
通過向?qū)薷谋粍?chuàng)建的代碼來添加一個圖標(biāo)或一些文本:
MessageDialog { id: aboutDialog icon: StandardIcon.Information title: qsTr("About") text: "Qt Quick UI Forms" informativeText: qsTr("This example demonstrates how to separate the " + "implementation of an application from the UI " + "using ui.qml files.") }
從您創(chuàng)建的Help菜單中啟動訪問About對話框。
運(yùn)行應(yīng)用程序
該應(yīng)用程序已經(jīng)完成,隨時可以在桌面上運(yùn)行或部署到設(shè)備上。要運(yùn)行應(yīng)用程序,按Ctrl+ R。
文件:
- uiforms/CustomerModelSingleton.qml
- uiforms/CustomerTableView.qml
- uiforms/History.qml
- uiforms/HistoryTableView.qml
- uiforms/MainForm.ui.qml
- uiforms/Notes.qml
- uiforms/NotesForm.ui.qml
- uiforms/Settings.qml
- uiforms/SettingsForm.ui.qml
- uiforms/main.qml
- uiforms/qml.qrc
- uiforms/uiforms.pro