一、基本使用
1、QML
function setValue(value)
{
if(value>1 || value<0)
{
return;
}
pen.score = value;
pen.requestPaint();
}
2、C++
#include <QMetaObject>
QObject *root = ui->quickWidget->rootObject();
QMetaObject::invokeMethod(root, "setValue", Q_ARG(QVariant, QVariant(0.8)));
Q_ARG是入参,如果需要接收返回值用Q_RETURN_ARG(QVariant, returnedValue)
二、访问QML里的函数
如qml里有多个方块,每个方块有多个属性,怎么在c++里设置这些属性呢?可以通过:
①、动态绑定每一个参数【很麻烦,工作量也大】;
②、把每个方块的信息存入json里,再通过objectname获取到每一个方块,再通过function传入json字符串,由qml在function内部完成json解析获取对应参数;
③、。。
这里使用第二种方式
1、封装每个方块,主要由两个参数:nodeid和name
MyNode.qml
import QtQuick 2.0
Rectangle {
id: