QML与C++交互

目录

1 QML获取C++的变量值

2 QML获取C++创建的自定义对象

3 QML发送信号绑定C++端的槽

4 C++端发送信号绑定qml端槽

5 C++调用QML端函数


1 QML获取C++的变量值
QQmlApplicationEngine engine;

全局对象

上下文属性

QQmlApplicationEngine engine;
QQmlContext *context1 = engine.rootContext();
context1->setContextProperty("test",200);

在qml中可全局直接使用test 

2 QML获取C++创建的自定义对象

myobject.h

#ifndef MYOBJECT_H
#define MYOBJECT_H

#include 

class MyObject : public QObject
{
    Q_OBJECT
public:
    explicit MyObject(QObject *parent = nullptr);

    int iValue() const;
    void setIValue(int newIValue);

    const QString &sString() const;
    void setSString(const QString &newSString);

signals:
    void iValueChanged();

    void sStringChanged();

private:
    int m_iValue;
    QString m_sString;

    Q_PROPERTY(int iValue READ iValue WRITE setIValue NOTIFY iValueChanged)
    Q_PROPERTY(QString sString READ sString WRITE setSString NOTIFY sStringChanged)
};

#endif // MYOBJECT_H

myobject.c

#include "myobject.h"

MyObject::MyObject(QObject *parent)
    : QObject{parent}
{

}

int MyObject::iValue() const
{
    return m_iValue;
}

void MyObject::setIValue(int newIValue)
{
    if (m_iValue == newIValue)
        return;
    m_iValue = newIValue;
    emit iValueChanged();
}

const QString &MyObject::sString() const
{
    return m_sString;
}

void MyObject::setSString(const QString &newSString)
{
    if (m_sString == newSString)
        return;
    m_sString = newSString;
    emit sStringChanged();
}

mian.c

qmlRegisterType("testObj",1,0,"MyObject");

main.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.5
import testObj 1.0
Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    MyObject {
        iValue: 20
        sString: "test"

    }


}

使用c++端的函数时需要在函数前面加上 Q_INVOKABLE

3 QML发送信号绑定C++端的槽

先注册 qmlRegisterType("testObj",1,0,"MyObject");

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.5
import testObj 1.0
Window {
    id: window
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    MyObject {
        id: myobj
        iValue: 20
        sString: "test"

    }
    
    signal qml_signal(string str)
    Button {
        onClicked: {
            qml_signal("qml send signal test");
        }
    }
    //方法一
//    Connections {
//        target: window
//        function onQml_signal(str){
//            myobj.qml_slot(str)
//        }
//    }
    //方法二
    Component.onCompleted: {
        qml_signal.connect(myobj.qml_slot)
    }

    //方法三 mian.c 中engine load后
    //auto obj_list = engine.rootObjects();
    //auto window = list.first();
    //connect(window,SIGNAL(qml_signal(QString)),your function ptr,SLOT(qml_slot(QString)));

}
4 C++端发送信号绑定qml端槽

myobject.h

信号 void signal_Cpp(QString str); 通过函数

Q_INVOKABLE void myFunction();发送
#ifndef MYOBJECT_H
#define MYOBJECT_H

#include 

class MyObject : public QObject
{
    Q_OBJECT
public:
    explicit MyObject(QObject *parent = nullptr);

    int iValue() const;
    void setIValue(int newIValue);

    const QString &sString() const;
    void setSString(const QString &newSString);

    Q_INVOKABLE void myFunction();

    static MyObject* getInstance();
public slots:
    void qml_slot(QString str);
signals:
    void iValueChanged();

    void sStringChanged();

    void signal_Cpp(QString str);
private:
    int m_iValue;
    QString m_sString;

    Q_PROPERTY(int iValue READ iValue WRITE setIValue NOTIFY iValueChanged)
    Q_PROPERTY(QString sString READ sString WRITE setSString NOTIFY sStringChanged)
};

#endif // MYOBJECT_H

myobject.c

#include "myobject.h"
#include 
MyObject::MyObject(QObject *parent)
    : QObject{parent}
{

}

int MyObject::iValue() const
{
    return m_iValue;
}

void MyObject::setIValue(int newIValue)
{
    if (m_iValue == newIValue)
        return;
    m_iValue = newIValue;
    emit iValueChanged();
}

const QString &MyObject::sString() const
{
    return m_sString;
}

void MyObject::setSString(const QString &newSString)
{
    if (m_sString == newSString)
        return;
    m_sString = newSString;
    emit sStringChanged();
}

void MyObject::myFunction()
{
    emit signal_Cpp("signal_Cpp myFunction!");
}

MyObject *MyObject::getInstance()
{
    static MyObject* obj = new MyObject();
    return obj;
}

void MyObject::qml_slot(QString str)
{
    qDebug()<<"qml_slot"<

mian.c

注册单例类

qmlRegisterSingletonInstance("testObj",1,0,"MyObject",MyObject::getInstance());
#include 
#include 
#include 
#include 
#include "myobject.h"

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

    QQmlApplicationEngine engine;
    QQmlContext *context1 = engine.rootContext();
    context1->setContextProperty("test",200);
    //qmlRegisterType("testObj",1,0,"MyObject");
    
    qmlRegisterSingletonInstance("testObj",1,0,"MyObject",MyObject::getInstance());

    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.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.5
import testObj 1.0
Window {
    id: window
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

//    MyObject {
//        id: myobj
//        iValue: 20
//        sString: "test"

//    }

    function qmlSolt(str) {
        console.log(str)
    }

    signal qml_signal(string str)
    Button {
        onClicked: {
            //C++发送信号
            MyObject.myFunction()
        }
    }

    Connections {
        target: MyObject
        function onSignal_Cpp(str) {
            //qml槽函数
            qmlSolt(str)
        }
    }


}
5 C++调用QML端函数

mian.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.5
import testObj 1.0
Window {
    id: window
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")



    function qmlFunction(arg1,arg2) {
            return arg1 + arg2;
    }


}

mian.c

#include 
#include 
#include 
#include 
#include "myobject.h"

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

    QQmlApplicationEngine engine;
    QQmlContext *context1 = engine.rootContext();
    context1->setContextProperty("test",200);
    qmlRegisterType("testObj",1,0,"MyObject");

    //qmlRegisterSingletonInstance("testObj",1,0,"MyObject",MyObject::getInstance());

    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);
    //获取qml对象
    auto obj_list = engine.rootObjects();
    auto window = obj_list.first();

    QVariant res;
    QVariant arg1 = "arg1";
    QVariant arg2 = "arg2";
    //调用QML函数
    QMetaObject::invokeMethod(window,"qmlFunction",
                              Q_RETURN_ARG(QVariant,res),
                              Q_ARG(QVariant,arg1),
                              Q_ARG(QVariant,arg2)
                              );
    qDebug()<

你可能感兴趣的:(QT,#,QML,qt,qml)