Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Correct way to quit application. How?



  • Hello all!

    I've got issue with destroying objects in application at time of exit. A have kind of backend object that is outside of QQmlApplicationEngine. When I am exiting application I've got this error in QML:

    qrc:/Mobile/qml/Main/Mobile.qml:67: TypeError: Cannot call method 'SomeMethod' of null
    

    I understand that object that is called from QML destroying first. How to make be destroyed after destroying QQmlApplicationEngine? Is there any signal in QQmlApplicationEngine that is showing "died"? I found only signal quit().

    Or should I be catching quit event somehow and destroy it manually?


  • Moderators

    Simply parent your backend to the engine. Then it will get cleaned up correctly and at the right moment.

    Something like (pseudo code):

    QQmlApplicationEngine engine;
    Backend *backend = new Backend(&engine);
    


  • @sierdzio thx will try tomorrow.



  • @sierdzio I do something missing. Still have this troubles. My backend is Singletone. Can it be source of the problem?



  • @sierdzio Issue solved. Thx for assistance. The problem was happening because I've been starting QQmlEngine inside of another object, not in main. When all of it started in main and backend is after engine - all is working perfectly.

    Something like this:

    int main(int inCounter, char *inArguments[]) {
    
    	QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    	QGuiApplication oApplication(inCounter, inArguments);
    	QQmlApplicationEngine oEngine;
    
    	Backend* oBackend = &Backend::mInstance(
    		&oApplication,&oEngine,oEngine.rootContext()
    	);
    	oBackend->mInit();
    
    	const QUrl oURL(QStringLiteral(MOBILE_QML_MAIN));
    	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();
    }
    

    I've tried to put it inside of wrapper together with backend where all of them has one parent. Got failed. When both of them in main - all is OK.


Log in to reply