本页内容

教程:Qt Quick 应用程序

本教程将演示如何使用 Qt Extension for VS Code 来创建一个 Qt Quick 应用程序。您将使用项目向导创建一个项目,并利用Qt Quick 控件设计用户界面。此外,您还将学习如何向项目中添加QML文件。

您可以从Code: QuickAddressBook 下载该完成的项目。

{QuickAddressBook 应用程序}

开始之前

开始之前,您需要:

创建一个Qt Quick 应用程序项目

要在 Visual Studio Code 中创建一个Qt Quick 项目:

  1. 转到“Welcome ”,然后选择“New project ”。
  2. 在Project 中,选择 Qt Quick application。

    {“新建项目”视图中的一个 <span translate=Qt Quick 应用程序}" src="images/vscodeext-create-new-project-quick.webp" title="{“新建项目”视图中的一个 Qt Quick 应用程序}"/>

  3. 在Name 中,输入QuickAddressBook 。
  4. 在“Create in ”中,输入项目文件的路径。
  5. 选择“Create ”以创建项目文件。
  6. 为该项目选择一个 CMake 套件。

现在,您已拥有一个可运行的小型Qt Quick 应用程序。选择“在终端窗口中启动所选项目 ”进行构建并运行。目前,运行结果是一个空窗口。

设计主窗口

向导已为您创建了一个主 QML 文件,其中声明了一个类型为 `ApplicationWindow` 的根对象。修改该文件以设计应用程序的主窗口。

要将组件绑定到其上下文,请指定Bound 参数:

pragma ComponentBehavior: Bound

在根类型中添加计划使用的模块的导入语句:

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

将title 属性的值设置为应用程序主窗口的标题:

ApplicationWindow {
    id: mainWindow
    visible: true
    width: 480
    height: 640
    title: qsTr("Address Book")

添加一个按钮

要创建“Add ”按钮,请声明一个来自Qt Quick Controls模块的 Button 类型对象。将按钮的text 属性值设置为Add... ,并将font.pointSize 属性值设置为24 :

Button {
    id: addButton
    text: "Add..."
    font.pointSize: 24

运行应用程序后,您应看到如下界面:

{QuickAddressBook 的主窗口}

将按钮连接到一个动作

QML 具有信号和处理程序机制,其中信号代表事件,而通过信号处理程序对信号作出响应。当信号被发出时,相应的信号处理程序会被调用。在处理程序中放置脚本或其他操作等逻辑,可使组件对事件作出响应。

若要在特定对象发出特定信号时接收通知,该对象的定义中应声明一个名为 `on<Signal>` 的信号处理程序,其中 `<Signal> ` 是信号的名称,首字母需大写。该信号处理程序应包含在被调用时需执行的 JavaScript 代码。

Button 类型具有一个名为clicked 的信号,该信号在用户点击按钮时发出。若要在用户在主窗口中选择Add 按钮时调出用于添加通讯录条目的弹出窗口,必须将该按钮的onClicked 信号处理程序连接到弹出窗口的open() 方法:

onClicked: newAddressPopup.open()

稍后,您将把该弹出窗口作为独立的 QML 类型添加进来。

添加列表模型

{“快速通讯录”条目}

在ApplicationWindow 中,声明一个ListModel 类型的对象,并使用id 作为addressList 来存储联系人数据。列表模型定义了一个自由格式的列表数据源:

ListModel {
    id: addressList
}

声明一个弹出窗口

声明一个自定义的NewAddressPopup 类型的对象,该对象定义了用户将在此添加通讯录条目的弹出窗口。使用onAddressAdded 信号处理程序将通讯录条目追加到addressList 模型中:

NewAddressPopup {
    id: newAddressPopup
    onAddressAdded: function(newName, newAddr) {
        addressList.append({name: newName, addr: newAddr})
}

您将在稍后创建NewAddressPopup 类型。

定位按钮

声明一个ColumnLayout 类型的对象,用于定位Button 对象和Repeater 类型的实例:

ColumnLayout {
    id: mainWindowLayout
    Layout.fillWidth: true
    spacing: 0
    Button {
        id: addButton
        Layout.preferredWidth: mainWindow.width
        Layout.fillWidth: true
        text: "Add..."
        font.pointSize: 24
        onClicked: newAddressPopup.open()
    }

将Layout.fillWidth 设置为true ,以使应用程序能够适应不同屏幕尺寸。将列中各行之间的spacing 设置为0 。将按钮的首选宽度绑定到主窗口的宽度。

添加一个重复器

Repeater 类型可创建多个相似的项目。它包含一个模型和一个委托:对于模型中的每个条目,委托都会在一个由模型数据初始化的上下文中被实例化。将 封装在ColumnLayout 等定位器类型的实例中,以视觉方式定位 创建的委托项目。

将 `addressList ` 指定为重复器要使用的 `model `:

Repeater {
    id: addressListViewer
    model: addressList
    Layout.preferredWidth: mainWindow.width
    Layout.fillWidth: true
}

声明一个自定义的 `AddressBookItem ` 类型的对象,重复器将使用该对象来实例化通讯录条目:

AddressBookItem {
    id: addressBookItem

您将在稍后创建AddressBookItem 类型。

将“删除”按钮连接到一个操作

使用onRemoved 信号处理程序,当用户点击其Remove 按钮时,从列表中删除通讯录条目。

onRemoved: addressList.remove(index)

添加弹出窗口

现在主窗口已准备就绪,接下来创建一个弹出窗口,供用户添加通讯录条目。AddressBookItem 类型指定了重复器将实例化用户在主窗口中输入的数据。

{QuickAddressBook 的弹出窗口}

在 Visual Studio Code 中使用 Qt 文件向导来创建定义该弹出窗口的自定义类型。

创建弹出窗口

要将自定义 QML 类型添加到项目中:

  1. 转到“Command Palette ”,然后选择“Qt: Create a new project or file ”。
  2. 在“File ”中,选择“QML file ”。
  3. 在“Name ”中,输入NewAddressPopup 。
  4. 在“Create in ”中,在项目文件夹路径后输入QuickAddressBookTypes ,以便在该子文件夹中创建新文件。
  5. 选择“Create ”以创建自定义 QML 类型。

在 CMakeLists.txt 文件的QML_FILES 部分中添加NewAddressPopup.qml :

qt_add_qml_module(appQuickAddressBook
    URI QuickAddressBook
    VERSION 1.0
    QML_FILES
        Main.qml
        QuickAddressBookTypes/AddressBookItem.qml
        QuickAddressBookTypes/NewAddressPopup.qml
)

设计弹出窗口

在 `NewAddressPopup.qml` 中,声明一个类型为 `Popup ` 的根对象,以创建一个在 `ApplicationWindow` 中打开的弹出窗口。弹出窗口本身不提供布局,因此您需要使用 `ColumnLayout ` 和 `RowLayout ` 来定位 `Name ` 和 `E-Mail Address ` 字段。

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

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
        Layout.preferredWidth: newAddressPopup / 2
        Layout.fillWidth: true
    }
    TextField {
        id: addrField
        placeholderText: qsTr("E-Mail Address")
        font.pointSize: 24
        Layout.preferredWidth: newAddressPopup / 2
        Layout.fillWidth: true
    }

按钮定位

使用RowLayout 类型的实例来定位两个Button 对象,这些对象分别指定Add 和Cancel 按钮:

RowLayout {
    Layout.fillWidth: true
    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() 方法:

    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 类型指定了在主窗口中如何呈现通讯录条目。

请按照“创建弹出窗口”中的说明,创建一个名为AddressBookItem.qml 的新 QML 文件,并将其添加到CMakeLists.txt 文件中。

设计条目

首先,您需要声明一个类型为 `Rectangle` 的根对象。这是在 QML 中创建应用程序可用的基本构建块之一。为其指定一个 `id `,以便稍后引用。

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

Rectangle {
    id: addressBookItem

设置将在Rectangle 和Text 类型中使用的属性:

required property int index
required property string name
required property string addr

若要使各行交替显示不同颜色,请设置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
    Layout.fillWidth: true
    RoundButton {
        id: deleteButton
        text: "??"
        font.pointSize: 12
        palette.buttonText: "red"
        onClicked: addressBookItem.removed()
    }

格式化文本

将text 属性的值设置为合并弹出窗口中name 和addr 字段的值,并对这些值应用粗体和斜体格式:

Text {
    id: itemText
    font.pointSize: 24
    text: "<b>" + addressBookItem.name + "</b><br><i>" + addressBookItem.addr + "</i>"
}

您的应用程序现已完成。

另请参阅 “创建 Qt 项目”、“向项目添加文件”和“启用QML Language Server ”。

© 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.