-
Notifications
You must be signed in to change notification settings - Fork 364
/
CppObject.cpp
39 lines (34 loc) · 1.02 KB
/
CppObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "CppObject.h"
#include <QDebug>
CppObject::CppObject(QObject *parent)
: QObject(parent)
{
}
QString CppObject::cppFunc(int i)
{
qDebug()<<"cpp function run"<<i;
return "cpp return value";
}
void CppObject::doInvoke(int i, QQuickItem *item)
{
qDebug()<<"cpp doInvoke"<<i;
QVariant val_return; //返回值
//Q_RETURN_ARG()和Q_Arg()参数必须制定为QVariant类型
QMetaObject::invokeMethod(item,
"qmlFunc",
Q_RETURN_ARG(QVariant, val_return),
Q_ARG(QVariant, i));
qDebug()<<"QMetaObject::invokeMethod ret:"<<val_return;
}
void CppObject::doSomething(int i, QJSValue callback)
{
qDebug()<<"cpp doSomething"<<i;
QJSValueList arglist;
arglist.append(callback.engine()->toScriptValue(i));
arglist.push_back(QJSValue(true));
if(callback.isCallable()){
//回调qml中的函数
auto ret = callback.call(arglist);
qDebug()<<"cpp callback ret:"<<ret.toString();
}
}