在QML系统中注册C++类型

0x00

#ifndef BIGDATAINFO_H
#define BIGDATAINFO_H

#include 
#include 
#include 

class BigDataInfo : public QObject
{
    Q_OBJECT
    Q_PROPERTY(qint32 _id READ getId WRITE setId NOTIFY idChanged)
    Q_PROPERTY(QString _name READ getName WRITE setName NOTIFY nameChanged)
    Q_PROPERTY(QDate _date READ getDate WRITE setDate NOTIFY dateChanged)

public:
    explicit BigDataInfo(QObject *parent = nullptr);
    ~BigDataInfo();

signals:
    void idChanged(qint32 id);
    void nameChanged(QString name);
    void dateChanged(QDate date);

public slots:

public:
    void setId(qint32 id)
    {
        if (m_id == id)
            return;

        m_id = id;
        emit idChanged(m_id);
    }

    void setName(QString name)
    {
        if (m_name == name)
            return;

        m_name = name;
        emit nameChanged(m_name);
    }

    void setDate(QDate date)
    {
        if (m_date == date)
            return;

        m_date = date;
        emit dateChanged(m_date);
    }

public:
    qint32 getId() const
    {
        return m_id;
    }

    QString getName() const
    {
        return m_name;
    }

    QDate getDate() const
    {
        return m_date;
    }

private:
    qint32 m_id;
    QString m_name;
    QDate m_date;
};

#endif // BIGDATAINFO_H

#include "bigdatainfo.h"

BigDataInfo::BigDataInfo(QObject *parent) : QObject(parent)
{

}

BigDataInfo::~BigDataInfo()
{

}

0x01

import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.12
import com.HLD 1.0

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("C++数据")

    Column{
        anchors.fill: parent;
        spacing: 15;

        Button{
            text: "测试";

            onClicked: {
                input.text += ("序号:" + _data._id);
                input.text += "\r\n";
                input.text += ("姓名:" + _data._name);
                input.text += "\r\n";
                input.text += ("时间:" + Qt.formatDateTime(_data._date, "yyyy-MM-dd hh:mm:ss.zzz"));
            }
        }

        Rectangle{
            width: parent.width;
            height: 150;
            border.width: 2
            radius: 2
            border.color: "red";

            TextEdit{
                id: input;
                anchors.fill: parent;
                clip: true;
                wrapMode: TextEdit.Wrap;
                font.pointSize: 20
                color: "blue"
                focus: true
            }
        }
    }

    BigDataInfo{
        id: _data;
        _id: 1;
        _name: "HLD";
        _date: new Date();
    }
}

0x02

#include 
#include 
#include "bigdatainfo.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    // 在QML系统中注册C++类型
    qmlRegisterType<BigDataInfo>("com.HLD", 1, 0, "BigDataInfo");

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

你可能感兴趣的:(QML,c++,qt,QML)