Qt Quick Controls - 联系人列表
一个使用Qt Quick Controls 和 C++ 类实现简单联系人列表的 QML 应用程序。
联系人列表示例展示了如何以与Qt Quick Designer 兼容的方式集成 C++ 后端。对于用户界面的声明部分,使用了 .ui.qml 文件,可在Qt Quick Designer 中进行可视化编辑。
C++ 后端
联系人列表应用程序允许用户添加、编辑和删除联系人。实际实现是在 C++ 中完成的,并作为QAbstractListModel 公开。
ContactModel C++ 类在命名空间下注册,随后由MainForm.ui.qml 导入并实例化。有关将 C++ 类注册为 QML 类型的更多信息,请参阅从 C++ 定义 QML 类型。
#include <QtQml/qqml.h> ... qmlRegisterType<ContactModel>("Backend", 1, 0, "ContactModel"); ...
设计器支持
在设计器子目录中,我们创建了一个插件,用于替换Qt Quick Designer 中的 ContactModel。为此,我们在contactlist.pro 中添加以下一行。
QML_DESIGNER_IMPORT_PATH = $$PWD/designer
由于Qt Quick Designer 无法实例化 ContactModel C++ 类,我们使用ListModel 定义了一个模拟。这可确保使用该模型的ListView 在Qt Quick Designer 中显示一些内容。
运行示例
运行示例 Qt Creator,打开Welcome 模式并从Examples 中选择示例。更多信息,请参见Qt Creator: 教程:构建并运行。
© 2025 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.