Important: Please read the Qt Code of Conduct -

"Loader" functionality inside a Singleton QtObject

  • I am specifying (and accessing) app settings from a singleton QtObject defined in a Config.qml file. This is working just fine (more details here). But now I would like to have multiple config files corresponding to various products (e.g. ConfigProd1.qml, ConfigProd2.qml etc). In building the app for a different product, I would like to be able to just source a new ConfigProd.qml inside the Config.qml, so that the rest of the code can just refer to Config.qml without caring about what product it was configured for.

    I first thought perhaps I might use a Loader in the QtObject in Config.qml:

    import QtQuick 2.0
    pragma Singleton
    QtObject {
        objectName: "Config"
        Loader {
            source: "ConfigProd1.qml"

    where ConfigProd1.qml has

    import QtQuick 2.0
    QtObject {
        objectName: "ConfigProd1"
        readonly property int width: 37
        readonly property int height: 43

    but that didn't work. Presumably Loaders work only with visual items? My second thought was that it might be accomplished via a directory-listing qmldir file with the line

    Config ConfigProd1.qml

    but that doesn't work either, I think because I have to register the type explicitly as

    qmlRegisterSingletonType(QUrl(QStringLiteral("qrc:/AppConfig/Config.qml")), "com.test.Config", 1, 0, "Config");

    Is there any way to accomplish what I want?

  • For now, I have worked around this by symlinking the product config files to Config.qml thus:

    $ ln -sf ConfigProd1.qml Config.qml

    Qt Creator doesn't seem to have any problems with symlinked files, and VCSs (Mercurial in my case) seem to support them fine as well.

    Would still be nice to get an idiomatic QML technique to do this if there is one, but this works for now.

Log in to reply