admin管理员组文章数量:1244303
In C++ I have a class with an invokable function, what I would like to do is call that method from QML/Javascript (this I've gotten to work) and pass it a Javascript callback.
In code, I define my class like:
class MyObject: public QObject
{
Q_OBJECT
public:
Q_INVOKABLE void doSomething(quint64 x, /* what goes here? */ jsCallback)
{
x += 1;
// I suspect this will require a invocation mechanism but
// this shows what I'd like to do
jsCallback(x);
}
};
And in my QML, I would like to do something like:
Rectangle {
function myCallback(x){
console.log("x=" + x);
}
MouseArea{
anchors.fill: parent
onClicked:{
myObject.doSomething(2, myCallback);
}
}
}
So that when I click on the Rectangle
, I would see x=3
in the console. How would I define the parameter in C++ and invoke the callback in order to acplish this?
Thank you!
In C++ I have a class with an invokable function, what I would like to do is call that method from QML/Javascript (this I've gotten to work) and pass it a Javascript callback.
In code, I define my class like:
class MyObject: public QObject
{
Q_OBJECT
public:
Q_INVOKABLE void doSomething(quint64 x, /* what goes here? */ jsCallback)
{
x += 1;
// I suspect this will require a invocation mechanism but
// this shows what I'd like to do
jsCallback(x);
}
};
And in my QML, I would like to do something like:
Rectangle {
function myCallback(x){
console.log("x=" + x);
}
MouseArea{
anchors.fill: parent
onClicked:{
myObject.doSomething(2, myCallback);
}
}
}
So that when I click on the Rectangle
, I would see x=3
in the console. How would I define the parameter in C++ and invoke the callback in order to acplish this?
Thank you!
Share Improve this question edited Jul 30, 2015 at 20:24 BaCaRoZzo 7,6928 gold badges53 silver badges86 bronze badges asked Jul 24, 2015 at 19:35 AddyAddy 2,5221 gold badge23 silver badges48 bronze badges1 Answer
Reset to default 17I think I've figured this out. What I ended up doing was implementing this in my C++ class like such:
class MyObject: public QObject
{
Q_OBJECT
public:
Q_INVOKABLE void doSomething(quint64 x, QJSValue jsCallback)
{
x += 1;
QJSValue val = jsCallback.engine()->newObject();
val.setProperty("x", x);
jsCallback.call(QJSValueList { val });
}
};
And then I can access the value in my callback like:
function myCallback(x){
console.log("x=" + x.x);
}
本文标签: qtPassing a Javascript callback to a C Invoked method in QmlStack Overflow
版权声明:本文标题:qt - Passing a Javascript callback to a C++ Invoked method in Qml - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740148891a2232290.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论