Qt使用教程之使用Qt Quick UI表单(六)

<Qt Enterprise最新版下载>

创建菜单

向导添加一个菜单栏到main.qml文件中,这其中包含了一个具有Open和Exit菜单的File菜单。保存菜单和Exit菜单项,然后添加具有标准菜单项的Edit和Help菜单。

该向导将创建下面的代码:

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菜单项并添加下面的代码来创建新的菜单:

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表单(六)_第1张图片

该向导将在main.qml文件文件中创建一个消息对话框:

MessageDialog {
id: messageDialog
title: qsTr( "May I have your attention, please?" )
 
function show(caption) {
messageDialog.text = caption;
messageDialog.open();
}

通过向导修改被创建的代码来添加一个图标或一些文本:

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

有兴趣的朋友可以点击查看更多有关Qt的文章

你可能感兴趣的:(Qt使用教程之使用Qt Quick UI表单(六))