Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Wait for loading object before load URL in QML application. How?



  • Hello all!
    I have the backend object that is requiring some time before first QML loading in QML Engine. How to run loading QML only after backend object is loaded? This backend registering in root-context some properties, and because it's appearing faster then backend i see this kind of error:

    ReferenceError: BackendObject is not defined
    

    I understand about slot-signal. I have in backend object kind of "finished" signal. To what slot it should be connected in Engine?

    I've tried to connect something like this:

    QGuiApplication oApplication(inCounter, inArguments);
    QQmlApplicationEngine oEngine;
    
    const QUrl oURL(QStringLiteral(QML_MAIN));
    
    Backend* oBackend = &Backend::mInstance();
    oEngine.rootContext()->setContextProperty("Backend",oBackend);
    oBackend->mInit(&oApplication,&oEngine,oEngine.rootContext());
    
    QObject::connect(
    	oBackend,&SBackend::sgInitiated,
    	[&oEngine,&oURL](){
    		oEngine.load(oURL);
    	}
    );
    
    return oApplication.exec();
    

    What am I missing?



  • Issue closed. Solution found - using QEventLoop before loading URL.
    Something like this:

    QGuiApplication oApplication(inCounter, inArguments);
    QQmlApplicationEngine oEngine;
    
    const QUrl oURL(QStringLiteral(QML_MAIN));
    
    QEventLoop oEventLoop;
    Backend* oBackend = &Backend::mInstance();
    oEngine.rootContext()->setContextProperty("Backend",oBackend);
    QObject::connect(
    	oBackend,&SBackend::sgInitiated,
    	&oEventLoop,&QEventLoop::quit
    );
    oBackend->mInit(&oApplication,&oEngine,oEngine.rootContext());
    oEventLoop.exec();
    
    QObject::connect(
    	&oEngine, &QQmlApplicationEngine::objectCreated,
    	&oApplication, [oURL](QObject *obj, const QUrl &objUrl) {
    		if (!obj && oURL == objUrl) {
    			QCoreApplication::exit(-1);
    		}
    	}, Qt::QueuedConnection
    );
    oEngine.load(oURL);
    
    return oApplication.exec();
    

Log in to reply