Reputation: 49
I have the following problem:
I wrote a qml-GUI and an interface class to communicate with some C++ code by connecting signals on the qml-side with slots on the c++ side. The event or button based trigger work fine, but I need one signal that has to be triggerd directly at startup. I tried it by using Component.onCompleted
from my ApplicationWindow
. Howevery,
the output "setInitDrone() called" is generated but
getInitDrone()
is never reached. The QT documentation says: "The order of running the onCompleted handlers is undefined."
Can I make sure that the signal has already been initialized when I'm trying to send it, or is there any other method instead of using Component.onCompleted?
Thanks for any help!
main.qml:
ApplicationWindow{
id: appWindow
visible: true
minimumHeight: 800
minimumWidth: 700
visibility: Window.Maximized
signal setInitDrone()
Component.onCompleted: {
setInitDrone()
print("setInitDrone() called")
}
}
qml_cpp_interface.cpp:
void Qml_cpp_interface::getInitDrone(){
qDebug() << "Cpp initDrone received";
flightserver.init();
}
groundstation.cpp:
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
//Connect with C++ code
QObject *item = engine.rootObjects().first();
Qml_cpp_interface qml_cpp_interface;
QObject::connect(item, SIGNAL(setInitDrone()), &qml_cpp_interface,SLOT(getInitDrone()));
return app.exec();
Upvotes: 2
Views: 5124
Reputation: 49289
You are doing it wrong, instead of accessing QML stuff from C++, you should be accessing C++ from QML.
Expose Qml_cpp_interface
to QML as it would make sense exposing a core logic interface. Since you are doing initialization, you don't even need a signal, since presumably, you are only going to initialize once, which is what it means to "initialize".
Then you can simply call the initialization via
Component.onCompleted: Qml_cpp_interface.getInitDrone()
Also, I don't see any valid reason to initialize from QML, I mean you can directly initialize from C++, even implicitly from Qml_cpp_interface
's constructor. So by the time your GUI loads, you are already initialized.
Upvotes: 3