教程:Qt Quick 应用程序
本教程说明了如何使用 Qt VS Tools 创建应用程序。 Qt Quick应用程序。您将使用项目向导创建项目,并设计Qt Quick UI。此外,您还将学习如何在项目中添加 QML 模块定义和 QML 文件。
开始之前
在开始之前,您必须
创建Qt Quick 应用程序项目
在 Visual Studio 中创建Qt Quick 应用程序项目:
- 转到File >New >Project 。
- 搜索 Qt Quick Application.
- 选择项目向导,然后选择Next 。
- 在Project name 中,输入QuickAddressBook,然后选择Create 。
- 要确认Welcome 对话框,请选择Next 。
- 要设置调试和发布构建配置,请单击Qt Modules ,然后选择 Qt Quick和 Qt Quick Controls模块包含在项目中:
Qt Quick 应用向导中选择 Qt 模块" src="images/qtvstools-qtquick-app-modules.webp" title="在 Qt Quick 应用向导中选择 Qt 模块"/>
- 选择Finish 创建项目。
现在您有了一个可运行的小型Qt Quick 应用程序。转到Build >Build Solution 来构建它,然后转到Debug >Start Without Debugging (或按Ctrl+F5)来运行它。现在,结果是一个空窗口。
设计主窗口
向导为你创建了一个主 QML 文件,其中声明了一个Window 类型的根对象。更改文件以设计应用程序的主窗口。
为 Windowcolor 和title 属性指定值,以设置应用程序主窗口的背景颜色和标题:
Window { id: mainWindow visible: true width: 480 height: 640 color: "darkgray" title: qsTr("Address Book")
添加按钮
要创建Add 按钮,请从Qt Quick Controls模块中声明一个Button 类型的对象。将按钮text 属性的值设置为Add,将font.pointSize 属性的值设置为24:
Button { id: addButton anchors.left: parent.left anchors.right: parent.right text: "Add..." font.pointSize: 24
运行应用程序时,您将看到以下内容:

将按钮连接到动作
QML 有一个信号和处理机制,其中信号是事件,信号通过信号处理器响应。当信号发出时,相应的信号处理程序就会被调用。将脚本或其他操作等逻辑放置在处理程序中,组件就能对事件做出响应。
要在特定对象发出特定信号时接收通知,对象定义应声明一个名为on<Signal> 的信号处理程序,其中<Signal> 是信号名称,第一个字母大写。信号处理程序应包含调用信号处理程序时要执行的 JavaScript 代码。
Button 类型有一个clicked 信号,在用户点击按钮时发出。要在用户选择主窗口中的Add 按钮时调用用于添加地址簿条目的弹出窗口,必须将按钮的onClicked 信号处理程序连接到弹出窗口的open() 方法。稍后您将把弹出窗口添加为单独的 QML 类型。
onClicked: newAddressPopup.open() }
添加列表模型

在 Window 中,用id addressList声明一个ListModel 类型的对象,用于存储联系人数据。列表模型定义了一个自由格式的列表数据源:
ListModel { id: addressList }
声明弹出窗口
声明一个自定义NewAddressPopup 类型的对象,用于定义用户添加地址簿条目的弹出窗口。使用onAddressAdded 信号处理程序将地址簿条目添加到addressList模型中:
NewAddressPopup { id: newAddressPopup onAddressAdded: addressList.append({name: newName, addr: newAddr}) }
稍后您将创建NewAddressPopup 类型。
定位按钮
声明ColumnLayout 类型的对象,以定位Button 对象和Repeater 类型的实例:
ColumnLayout { id: mainWindowLayout anchors.left: parent.left; anchors.right: parent.right spacing: 0 Button { id: addButton anchors.left: parent.left anchors.right: parent.right text: "Add..." font.pointSize: 24 onClicked: newAddressPopup.open() }
将列布局锚定到父对象的左右边缘,使应用程序可以在不同尺寸的屏幕上扩展。将spacing 设置为0。
添加中继器
Repeater 类型可创建许多类似的项目。它有一个模型和一个委托:对于模型中的每个条目,委托都会在一个包含模型数据的上下文中实例化。在ColumnLayout 等定位器类型的实例中包含中继器,以便直观地定位中继器创建的委托项。
指定addressList为中继器使用的model :
Repeater { id: addressListViewer model: addressList anchors.left: parent.left anchors.right: parent.right
声明一个自定义AddressBookItem 类型的对象,中继器将使用该对象实例化地址簿条目:
AddressBookItem { id: addressBookItem
稍后您将创建AddressBookItem 类型。
将删除按钮连接到操作
当用户点击Remove 按钮时,使用onRemoved 信号处理程序从列表中删除地址簿条目。
onRemoved: addressList.remove(index) }
添加弹出窗口
现在主窗口已经准备就绪,接下来创建弹出窗口,用户可以在弹出窗口中添加地址簿条目。AddressBookItem 类型指定中继器将用户在主窗口中输入的数据实例化。

使用 Visual Studio 中的 Qt 文件向导创建定义弹出窗口的自定义类型。
要创建自定义 QML 类型,必须先在项目中添加 QML 模块定义 (qmldir) 文件。
添加 QML 模块定义
QML 模块定义 (qmldir) 将每个自定义 QML 类型映射到其相应的源文件。
要添加 QML 模块定义,请访问Project >Add New Item >Installed >Visual C++ >Qt >Qt QML Module Definition >Add 。
在qmldir 文件中,为AddressBookItem和NewAddressPopup 添加 QML 类型定义:
AddressBookItem 1.0 AddressBookItem.qml NewAddressPopup 1.0 NewAddressPopup.qml
接下来,您将创建 QML 类型。
创建弹出窗口
向项目中添加自定义 QML 类型:
- 转到Project >Add New Item >Installed >Visual C++ >Qt >Qt QML File 。
- 选择Add 。
- 在Name 中,输入NewAddressPopup。
- 选择Finish ,创建自定义 QML 类型。
设计弹出窗口
在NewAddressPopup.qml 中,声明一个Popup 类型的根对象,创建一个在Window 中打开的弹出窗口。弹出窗口本身不提供布局,因此您将使用ColumnLayout 和RowLayout 来定位Name 和E-Mail Address 字段。
Popup { id: newAddressPopup
将modal 属性设置为true ,以指定弹出窗口为模态窗口。将focus 属性设置为true ,以指定弹出窗口请求焦点:
modal: true focus: true
为width 、x 和y 属性指定值,以设置弹出窗口在主窗口顶部的位置和大小:
width: parent.width * 0.9 x: (parent.width - width) / 2 y: 35
重置弹出窗口控件
弹出窗口打开时,Name 和E-Mail Address 字段应显示占位符文本,之前输入的任何值都应清除。您可以使用onOpened 信号处理程序重置字段的值,并将焦点放在Name 字段上:
onOpened: { nameField.text = ""; addrField.text = ""; nameField.focus = true; }
位置字段
使用ColumnLayout 类型的实例来定位TextField 对象,该对象指定了弹出窗口的Name 和E-Mail Address 字段:
ColumnLayout { anchors.fill: parent TextField { id: nameField placeholderText: qsTr("Name") font.pointSize: 24 background: Rectangle { color: "lightgray" } Layout.preferredWidth: newAddressPopup / 2 Layout.fillWidth: true } TextField { id: addrField placeholderText: qsTr("E-Mail Address") font.pointSize: 24 background: Rectangle { color: "lightgray" } Layout.preferredWidth: newAddressPopup / 2 Layout.fillWidth: true }
定位按钮
使用RowLayout 类型的实例定位两个Button 对象,这两个对象分别指定Add 和Cancel 按钮:
RowLayout { anchors.left: parent.left; anchors.right: parent.right Button { text: "Add" enabled: nameField.length > 0 && addrField.length > 0 font.pointSize: 24 Layout.preferredWidth: newAddressPopup / 2 Layout.fillWidth: true
将按钮连接到操作
当用户单击Add 按钮时,他们在Name 和E-Mail Address 字段中输入的值将添加到主窗口的地址列表中,弹出窗口将关闭。
要启用此功能,请添加addressAdded(string newName, string newAddr) 信号:
signal addressAdded(string newName, string newAddr)将Add 按钮的onClicked 信号处理程序连接到addressAdded() 信号和弹出窗口的close() 方法:
Button { text: "Add" enabled: nameField.length > 0 && addrField.length > 0 font.pointSize: 24 Layout.preferredWidth: newAddressPopup / 2 Layout.fillWidth: true onClicked: { newAddressPopup.addressAdded(nameField.text, addrField.text) newAddressPopup.close() } }
对于Cancel 按钮,将onClicked 信号处理程序连接到弹出窗口的close() 方法,以便在不保存数据的情况下关闭弹出窗口:
Button { text: "Cancel" font.pointSize: 24 Layout.preferredWidth: newAddressPopup / 2 Layout.fillWidth: true onClicked: newAddressPopup.close() }
定义地址簿条目
自定义AddressBookItem 类型指定了如何在主窗口中显示地址簿条目。
转到Project >Add New Item >Installed >Visual C++ >Qt >Qt QML File 并选择Add ,创建一个名为AddressBookItem.qml 的新 QML 文件。
设计条目
首先,你将声明一个Rectangle 类型的根对象。它是 QML 创建应用程序的基本构件之一。给它一个id ,以便以后引用。
Rectangle { id: addressBookItem
要在行中使用交替颜色,请设置color 属性的值:
color: (index % 2) == 0 ? "dimgray" : "lightgray"
将矩形锚定到父对象的左右边缘,使应用程序可在不同尺寸的屏幕上伸缩。将矩形height 属性绑定到它将包含的文本项的高度:
anchors.left: parent.left anchors.right: parent.right height: itemText.height + 12
将移除按钮连接到动作
添加removed() 信号,将其连接到移除按钮的 onClicked 信号处理程序。当用户单击该按钮时,将从主窗口中删除地址簿条目:
signal removed()定位按钮和文本
在RowLayout 类型的实例中使用RoundButton 和Text 类型的实例来定义地址簿条目:
RowLayout { spacing: 12 anchors.left: parent.left anchors.leftMargin: spacing RoundButton { id: deleteButton text: "🗙" font.pointSize: 12 palette.buttonText: "red" onClicked: addressBookItem.removed() }
格式化文本
设置text 属性的值,合并弹出窗口中name 和addr 字段的值,并对值使用粗体和斜体格式:
Text { id: itemText font.pointSize: 24 text: "<b>" + name + "</b><br><i>" + addr + "</i>" }
至此,您的应用程序就完成了。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.