本页内容

Qt Quick Controls - 联系人列表

一个使用Qt Quick Controls 和C++类的QML应用程序,该类实现了一个简单的联系人列表。

“联系人列表”示例演示了如何以与Qt Quick Designer兼容的方式集成C++后端。对于用户界面的声明式部分,使用了.ui.qml文件,这些文件可在Qt Quick Designer中进行可视化编辑。

显示姓名和地址的联系人列表应用程序

C++ 后端

联系人列表应用程序允许用户添加、编辑和删除联系人。实际实现采用 C++ 完成,并作为QAbstractListModel 对外暴露。

C++ 类 ContactModel 注册在一个命名空间下,随后由MainForm.ui.qml 导入并实例化。有关将 C++ 类注册为 QML 类型的更多信息,请参阅《从 C++ 定义 QML 类型》。

#include <QtQml/qqml.h>
...
qmlRegisterType<ContactModel>("Backend", 1, 0, "ContactModel");
...

设计器支持

在 designer 子目录中,我们创建了一个插件,用于在Qt Quick Designer 中替换 ContactModel。为使该功能生效,我们在contactlist.pro 中添加以下代码行:

QML_DESIGNER_IMPORT_PATH = $$PWD/designer

由于Qt Quick Designer无法实例化ContactModel C++类,因此我们使用ListModel 定义了一个模拟对象。这可确保在Qt Quick Designer中,使用该模型的ListView 能显示相应内容。

运行示例

您可以从以下位置运行示例:

示例项目 @ code.qt.io

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