其他分享
首页 > 其他分享> > QML  附加属性

QML  附加属性

作者:互联网

QML  附加属性

  • 参考视频:https://www.bilibili.com/video/BV1Ay4y1W7xd?t=1347&p=82
  • 使用Qt5.15.2版本,5.14缺少很多宏定义
  • 在一个类中附加另一个类,那么这个类就可以访问另一个类的属性。
    • 给Button附加了三个属性(name,age,date 附加属性),实际上就是在Button中增加了一个附加属性的对象。
    • 这个附加属性对象就是new 出来的(调用 qmlAttachedProperties)
  • 项目结构:

  • person.h
#ifndef PERSON_H
#define PERSON_H

#include <QObject>
#include <QString>
#include <QDate>
#include <QtQml>

class Info : public QObject
{
    Q_OBJECT
    QML_ANONYMOUS
    //将属性注册到元系统,不然qml中无法识别
    Q_PROPERTY(QString name READ getName WRITE setName NOTIFY nameChanged)
    Q_PROPERTY(qint32 age READ getAge WRITE setAge NOTIFY ageChanged)
    Q_PROPERTY(QDate date READ getDate WRITE setDate NOTIFY dateChanged)

public:
    using QObject::QObject;

    void setName(QString name) noexcept;
    QString getName()const noexcept;

    void setAge(qint32 age) noexcept;
    qint32 getAge() const noexcept;

    void setDate(QDate date) noexcept;
    QDate getDate() const noexcept;

signals:
    void nameChanged();
    void ageChanged();
    void dateChanged();

private:
    QString   m_strName;
    qint32    m_iAge;
    QDate     m_qDate;
};



class Person : public QObject
{
    Q_OBJECT

    QML_ATTACHED(Info)   //添加附加的类
    QML_ELEMENT
public:
    using QObject::QObject;

    static Info* qmlAttachedProperties(QObject*); // 必须添加,否则报错

};

#endif // PERSON_H
  • person.cpp
#include "Person.h"
#include <QDebug>



void Info::setName(QString name) noexcept
{
    m_strName = qMove(name);
    emit nameChanged();
}

QString Info::getName() const noexcept
{
    return m_strName;
}

void Info::setAge(qint32 age) noexcept
{
     m_iAge = age;
     emit ageChanged();
}

qint32 Info::getAge() const noexcept
{
    return m_iAge;
}

void Info::setDate(QDate date) noexcept
{
    m_qDate = date;
    emit dateChanged();
}

QDate Info::getDate() const noexcept
{
    return m_qDate;
}

Info *Person::qmlAttachedProperties(QObject *object)
{
    qDebug() << __FUNCTION__ <<  "object:" <<object;
    return new Info(object);
}
  • main.cpp
#include "./Person.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QMetaObject>   //元对象系统
#include <QQmlContext>
#include <QDebug>
#include <QObject>
#include <QQmlEngine>

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QGuiApplication app(argc, argv);

    //注册到qml
     //qmlRegisterType<Info>("InfoModel", 1, 0, "Info");


    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();
}
  • main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
import QtQml 2.14
import InfoModel  1.0

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("QML与CPP")


    Button{
        objectName: "qmlButton"
        text: "clicked"

        Person.name: "Attached"
        Person.age:22
        Person.date:new Date()
        onClicked: {
            console.log(Person.name + " "+ Person.age +  "" + Qt.formatDateTime(Person.date))
            console.log(this)
        }
    }
}
  • 输出:

几个注意事项

  • 工程文件添加
CONFIG += c++11 qmltypes
QML_IMPORT_NAME = InfoModel
QML_IMPORT_MAJOR_VERSION = 1
  • 添加并实现函数:  static Info* qmlAttachedProperties(QObject*); 否则报如下错误:

  • 放在最后,否则无法生成元信息文件

  • 生成的元信息文件

  • 元信息中的附加属性

另一种使用附加属性的方式

  • 对前面的工程进行改变,将工程中的注册注释掉
  • 改变main.cpp
  • 通过 qmlRegisterUncreatableType 注册
#include "./Person.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QMetaObject>   //元对象系统
#include <QQmlContext>
#include <QDebug>
#include <QObject>
#include <QQmlEngine>

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QGuiApplication app(argc, argv);

    //注册到qml
    qmlRegisterUncreatableType<Person>("PersonModel", 1, 0, "Person", "  reason:Instance creation is not allowed");

    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();
}

 

标签:Info,const,noexcept,附加,QObject,Person,QML,include,属性
来源: https://blog.csdn.net/baidu_41388533/article/details/117091443