使用 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_OBJECT
    Q_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(messaging
    URI com.mycompany.messaging
    VERSION 1.0
    SOURCES
        message.cpp message.h
)

set_target_properties(messaging PROPERTIES
    CXX_STANDARD 17
    CXX_STANDARD_REQUIRED ON
)

target_link_libraries(messaging
    PRIVATE Qt6::Core Qt6::Qml
)

使用 qmake

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

messaging.pro 文件:

QT += core qml quick

CONFIG += qmltypes

HEADERS += 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.0

Rectangle {
    width: 400
    height: 300

    Message {
        id: message
        author: "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 文件中实例化和使用它。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值