创建菜单
向导添加一个菜单栏到main.qml文件中,这其中包含了一个具有Open和Exit菜单的File菜单。保存菜单和Exit菜单项,然后添加具有标准菜单项的Edit和Help菜单。
该向导将创建下面的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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菜单项并添加下面的代码来创建新的菜单:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
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()
}
|
创建对话框
![Qt使用教程:使用Qt Quick UI表单(六)](https://i-blog.csdnimg.cn/blog_migrate/ac1ee8d129fa42b38c9a294a73764089.jpeg)
该向导将在main.qml文件文件中创建一个消息对话框:
1
2
3
4
5
6
7
8
|
MessageDialog {
id: messageDialog
title: qsTr(
"May I have your attention, please?"
)
function show(caption) {
messageDialog.text = caption;
messageDialog.open();
}
|
通过向导修改被创建的代码来添加一个图标或一些文本:
1
2
3
4
5
6
7
8
9
|
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."
)
}
|
从您创建的Help菜单中启动访问About对话框。
运行应用程序
该应用程序已经完成,随时可以在桌面上运行或部署到设备上。要运行应用程序,按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