Search code examples
sqliteqmlqt5qt-quick

Qt Quick how to get object at right moment


I'm making an application GUI with qt quick 5.10. In my main.qml I call a Loader to show the first login page and a second page to give initial settings. I save these information in a SQLite database. I would to skip the loading of these two page if the relative informations ara present in the database, but I don't know how to initialize and read the database first of the Loader. Is there a solution to initialize and read the database as first thing and then load the Loader? I've tried to put the following

    Component.onCompleted: {
        JSDB.dbInit();
        JSDB.readUserData();
    }

in the objects that I have in main.qml (QtObject, virtual Keyboard and the Loader), once for each one, but it seems that the Loader is the first one to be completed.


Solution

  • Your question is not very clear, but I think you are asking how to delay the Loader from doing anything until after some condition has cleared. There are plenty of ways to do that. The simplest would be to bind the Loader's active property to some boolean flag. As long as that flag stays false, the Loader won't load anything. Something like this should work:

    Window {
        property bool needsLogin: false
    
        Component.onCompleted: {
            // Do database stuff...
            needsLogin = ???
        }
    
        Loader {
            source: "LoginPage.qml"
            active: needsLogin
        }
    }