Proper way for C++ code at startup signalize something to QML
-
Hello, I have migrated from old style QT into QtQuick recently and I am liking a lot, but I am still having some difficulties (for that reason there will be more than one Question on the same context posted by me)
I have an application that in startup time I check if local credentials are still valid, if they are the c++ engine behind scenes operate normally. IF they are not valid, I need to make the interface (QML) go to the login screen. The main code is as follows.
int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; auto *backend_engine = new aquila_backend(&app); engine.rootContext()->setContextProperty("aquila", backend_engine); const QUrl url(u"qrc:/aquila_companion/Main.qml"_qs); QObject::connect(&engine, &QQmlApplicationEngine::quit, &QGuiApplication::quit); QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.addImportPath(QCoreApplication::applicationDirPath() + "/backend"); engine.addImportPath(":/"); engine.load(url); QObject* obj = engine.rootObjects().first(); QObject::connect(backend_engine, SIGNAL(append_log(QVariant)), obj, SLOT(append_log(QVariant))); QObject::connect(backend_engine, SIGNAL(go_to_login_screen(QVariant)), obj, SLOT(go_to_login_screen(QVariant))); backend_engine->data_folder = defaultFolderPath(); backend_engine->load_credentials(); //PROBLEM HAPPENS HERE return app.exec(); }
The checkup is made at load_credentials. The problem is, the app is not executing at that moment so I cannot send a signal to the app (and the return of that credential online validation can vary). What is the correct idiomatic way to solve this issue? I do not want to freeze the whole application without an interface just waiting for the validation, but I see that I cannot risk to emit a signal to the App if the validation returns before the interface is loaded.