[英]QQuickWidget send signal from c++ to slot in QML
I have a application and I want make a little animation for it. 我有一个应用程序,我想为其制作一些动画。 I did a qml file and used QQuickWidget to open and show it in my display.
我做了一个qml文件,并使用QQuickWidget打开并将其显示在显示器中。 Now a I want make iteration between c++ and QML.
现在我想在c ++和QML之间进行迭代。 I want, for example, when a function in c++ is called, a ball move in my display.
我想,例如,当调用c ++中的函数时,显示屏上有一个球移动。 But I could not make a connection between c++ and qml.
但是我无法在c ++和qml之间建立连接。
Every help is welcome. 欢迎任何帮助。
A little part of my code: c++ 我的代码的一小部分:c ++
QQuickWidget *quickWidget = new QQuickWidget;
quickWidget->setSource(QUrl("qrc:/QML/main.qml"));
auto rootObject = quickWidget->rootObject();
// Connect C++ signal to QML slot
connect(this, SIGNAL(cppSignal()), rootObject, SLOT(qmlSlot()));
emit cppSignal();
QML QML
Rectangle {
id: tela
visible: true
width: 715
height: 77
color: '#E8E8E8'
// NumberAnimation {
// running: true
// target: bolinha
// property: "x"
// duration: 1000
// to: 600
// }
function qmlSlot() {
bolinha.visible= enabled
animBolinha.start();
}
}
enter image description here 在此处输入图片说明
What I can do to solve it? 我该怎么解决?
I am not sure if you can call a QML method from C++ code as you did. 我不确定是否可以像以前那样从C ++代码调用QML方法。
The recommended way from QT documentation is: QT文档中推荐的方法是:
All QML methods are exposed to the meta-object system. 所有QML方法都公开给元对象系统。 As the functions are exposed to meta-object system, you can use
QMetaObject::invokeMethod()
, to invoke the QML function. 由于这些函数已公开给元对象系统,因此可以使用
QMetaObject::invokeMethod()
来调用QML函数。
Probably in your case, you should call as said below (not tested). 可能是您的情况,您应按以下方式致电(未经测试)。
auto rootObject = quickWidget->rootObject();
QMetaObject::invokeMethod(rootObject, "qmlSlot");
Look documentation (search for Invoking QML Methods ) 查看文档 (搜索调用QML方法 )
As said in documentation, you can use Q_ARG
to pass the arguments and Q_RETURN_ARG
for receiving return arguments. 如文档中所述,您可以使用
Q_ARG
传递参数,并使用Q_RETURN_ARG
接收返回参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.