QML 与 C++交互 - 02 QML访问C++对象的属性与函数

版权声明:本文为博主原创文章,未经博主允许不得转载。欢迎与我交流:[email protected] https://blog.csdn.net/baidu_33850454/article/details/81914821

前言

本系列会介绍几种QML与C++进行数据交互的方法,包括信号槽的链接,QML调用C++类的方法等。
本文为第二篇:C++暴露方法到QML。可以点击这里访问官方示例。

详细描述

由于QML引擎与Qt元对象系统的紧密集成,可以从QML代码访问由QObject衍生的类适当公开的任何功能。这使得不用做太多改动,C ++数据和函数可以直接从QML访问。

任何QML代码都可以访问QObject派生类的实例的以下成员:

1、属性 property
2、信号函数 signals
3、公有槽函数和使用Q_INVOKABLE声明的函数 — 允许通过元对象系统调用它们。
(此外,如果已使用Q_ENUMS声明枚举,则可以使用枚举。)

1 、属性

为了最大程度地与QML进行互操作,任何可写的属性都应该具有关联的NOTIFY信号,只要属性值发生变化就会发出该信号。例如:
Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged)
这用来实现QML的属性绑定机制。
详情可参考这里 – 《QML 与 C++交互 - 01QML访问C++属性》

2、信号

可以从QML代码访问QObject派生类型的任何公共信号。
QML引擎自动为从QML使用的QObject派生类型的任何信号创建信号处理程序。信号传递的所有参数都可以通过参数名称在信号处理程序中获得。同样可以在第一篇中找到demo。
详情可参考这里 – 《QML 与 C++交互 - 01QML访问C++属性》

3、函数

任何QObject派生类型的方法都可以从QML代码访问,只要它是:

  • 标有Q_INVOKABLE()宏的公共方法
  • 一种public slots的方法

QML支持调用重载的C ++函数。如果有多个具有相同名称但不同参数的C ++函数,则将根据提供的参数的数量和类型调用正确的函数。
以下示例,点击窗口时会调用Q_INVOKABLE方法 Q_INVOKABLE void callClassFunction()
公有槽函数void slotSetAuthor()
message.h

#ifndef MESSAGE_H
#define MESSAGE_H

#include <QObject>
#include <QDebug>

class Message : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged)
public:
    void setAuthor(const QString &a) {
        qDebug() << __FILE__ << __func__ << __LINE__ << a;
        if (a != m_author) {
            m_author = a;
            emit authorChanged();
        }
    }
    QString author() const {
        qDebug() << __FILE__ << __func__ << __LINE__;
        return m_author;
    }

    Q_INVOKABLE void callClassFunction(){
        qDebug() << __FILE__ << __func__ << __LINE__;
    }

public slots:
    void slotSetAuthor(){
        qDebug() << __FILE__ << __func__ << __LINE__;
        static int i = 0;
        ++i;
        QString str = QString("Tim%1").arg(i);
        setAuthor(str);
    }

signals:
    void authorChanged();
private:
    QString m_author;
};
#endif // MESSAGE_H

QML 文件

import QtQuick 2.5
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    MouseArea {
        anchors.fill: parent
        onClicked: {
            console.log(qsTr('Clicked on background. Text: "' + msg.author + '"'))
            msg.author = msg.author + " hello world"
            msg.callClassFunction()
            msg.slotSetAuthor()
        }
    }


    Text {
        text: msg.author    // invokes Message::author() to get this value

        Component.onCompleted: {
            msg.author = "Jonah"  // invokes Message::setAuthor()
        }
    }
}

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <message.h>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    Message msg;
    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("msg",&msg);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

这里写图片描述

demo下载地址:https://download.csdn.net/download/baidu_33850454/10618500

猜你喜欢

转载自blog.csdn.net/baidu_33850454/article/details/81914821