C++可传递给QML的数据

一、传递QVariantList

注意:

typedef QList<QVariant> QVariantList

1、qml

function readValues(anArray, anObject) {
    for (var i = 0; i < anArray.length; i++ )
        console.log("Array item:", anArray[ i ])
    for (var prop in anObject) {
        console.log("Object item:", prop, "=", anObject[prop])
    }
}

2、C++

QVariantList listY;
listY << 2 << 4 << 4.5 << 5 << 8 << 12 << 18;
QMetaObject::invokeMethod(qw->rootObject(), "setValue",
Q_ARG(QVariant,QVariant::fromValue(listY)),
Q_ARG(QVariant,QVariant(info)));

总结:

//C++
QVariantList
//QML
var p = [1,2,3,4,5]

 

 

 

 

 

二、传递点的集合

1、QML传到C++

//QML
var p = [Qt.point(1,1), Qt.point(2,2)]
cppObject.updatePoint(p);


//C++ 
class CppObject {
    void updatePoint(QVector<QPointf>& p)
    {
        //...
    }
}

 2、C++传到QML

QMetaObject::invokeMethod(qw->rootObject(), "setValue",
Q_ARG(QVariant,QVariant::fromValue(listY)));

 

posted @ 2020-06-02 09:43  朱小勇  阅读(1716)  评论(0编辑  收藏  举报