当前位置: 首页 > news >正文

使用 QML 类型系统注册 C++ 类型

使用 QML_ELEMENT 和相关的注册宏来注册 C++ 类型到 QML 类型系统是一个非常强大的功能,它使得在 QML 中使用 C++ 类型变得非常方便。这里是一个详细的步骤说明,展示如何使用这些宏来注册一个 C++ 类,并确保它可以在 QML 中被正确使用。

步骤 1: 准备 C++ 类

首先,创建一个 C++ 类,例如 Message,并使用 QML_ELEMENT 宏进行标记。

message.h 文件:

#ifndef MESSAGE_H
#define MESSAGE_H#include <QObject>
#include <QDateTime>
#include <QtQml/qqmlregistration.h>class Message : public QObject
{Q_OBJECTQ_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged)Q_PROPERTY(QDateTime creationDate READ creationDate WRITE setCreationDate NOTIFY creationDateChanged)QML_ELEMENT // 这会注册类为 QML 类型public:explicit Message(QObject *parent = nullptr);QString author() const;void setAuthor(const QString &author);QDateTime creationDate() const;void setCreationDate(const QDateTime &date);signals:void authorChanged();void creationDateChanged();private:QString m_author;QDateTime m_creationDate;
};#endif // MESSAGE_H

message.cpp 文件:

#include "message.h"Message::Message(QObject *parent): QObject(parent)
{
}QString Message::author() const {return m_author;
}void Message::setAuthor(const QString &author) {if (m_author != author) {m_author = author;emit authorChanged();}
}QDateTime Message::creationDate() const {return m_creationDate;
}void Message::setCreationDate(const QDateTime &date) {if (m_creationDate != date) {m_creationDate = date;emit creationDateChanged();}
}

步骤 2: 配置项目文件

使用 CMake
在 CMakeLists.txt 文件中,使用 qt_add_qml_module 来注册 QML 模块,并指定适当的 URI 和版本号。

CMakeLists.txt 文件:

cmake_minimum_required(VERSION 3.16)project(messaging)find_package(Qt6 REQUIRED COMPONENTS Core Qml)qt_add_qml_module(messagingURI com.mycompany.messagingVERSION 1.0SOURCESmessage.cpp message.h
)set_target_properties(messaging PROPERTIESCXX_STANDARD 17CXX_STANDARD_REQUIRED ON
)target_link_libraries(messagingPRIVATE Qt6::Core Qt6::Qml
)

使用 qmake

如果使用 qmake,需要在 .pro 文件中添加 QML 类型相关的配置。

messaging.pro 文件:

QT += core qml quickCONFIG += qmltypesHEADERS += message.h
SOURCES += message.cpp# Define the QML module
QML_IMPORT_NAME = com.mycompany.messaging
QML_IMPORT_MAJOR_VERSION = 1
QML_ADDED_IN_MINOR_VERSION = 0

步骤 3: 在 QML 中使用 C++ 类

现在,Message 类已经注册为 QML 类型,可以在 QML 文件中直接使用。

main.qml 文件:

import QtQuick 2.15
import com.mycompany.messaging 1.0Rectangle {width: 400height: 300Message {id: messageauthor: "Amelie"creationDate: new Date()}Text {text: "Author: " + message.author + ", Created on: " + message.creationDate.toLocaleString()anchors.centerIn: parent}
}

总结

创建 C++ 类:定义类并使用 QML_ELEMENT 宏。
配置项目文件:使用 CMake 或 qmake 配置项目,使其能够生成 QML 类型注册信息。
在 QML 中使用:导入在 C++ 中注册的 QML 类型并在 QML 文件中实例化和使用它。


http://www.mrgr.cn/news/10419.html

相关文章:

  • Threejs三要素及demo
  • 高标准农田灌区对农业发展的支撑作用
  • 你没有使用过(但应该使用)的前 5 个 HTML 功能
  • 8.20-ansible剧本的使用+roles的应用
  • Spring Security框架的学习
  • Spring中的AopUtils
  • Go小技巧易错点100例(十六)
  • 【网络安全】服务基础阶段——第一节:Windows系统管理基础----进制转换与IP地址
  • 【C语言】结构体
  • (每日一问)设计模式:设计模式的原则与分类——如何提升代码质量?
  • 数据结构之树体系:二叉树、平衡二叉树、红黑树、AVL树、B树、B+树、最小生成树、哈夫曼树、决策树、LSM树、后缀树、R树
  • 【机器学习】2. 数据预处理
  • Flask中的session
  • C# 使用RestSharp 开发WebApi client端,实现MES通信
  • 【漏洞复现】SuiteCRM responseEntryPoint Sql注入漏洞
  • mac安装java17(jdk17)
  • Spring Boot密码加密
  • Agent实际落地的应用 未来生活的无形助手
  • 基于改进YOLOv8的景区行人检测算法
  • 数据结构-串-了解串-串的基本操作