[Solved] Singletons in 5.2

  • Hey, i am afraid i am super confused as to how Singleton types are supposed to work.

    The documentation on the area is sparse at best and when there is some, it's all saying something different or something plain wrong. There has been a few discussions on this board dealing with this, and i can't seem to make sense of it.

    First of, the easiest way to declare a QML Singleton from within QML should be:
    //inside SomeSingleton.qml
    @pragma Singleton
    import QtQuick 2.2
    import QtQml 2.0

    QtObject{property myInt: 10}@

    //Inside SomeRect.qml
    @Rectangle{width : SomeSingleton.myInt}@

    Now, this simply doesn't work. - replacing pragma with .pragma, or using a qmldir to describe the module doesn't help either way or any documented
    ways. For reference:

    Also in new features page of 5.2 (http://qt-project.org/doc/qt-5/whatsnew52.html ) It States:
    Added ability to define QML singletons with QML files

    • The existing documentation on Styling/Singletons however are from before 5.2 - which lead me to believe some change might have happened in the way they work/are supposed to work.

    Looking at the forums, it's clear other people have had problems with this too. One "Solution" i found was that, you have to register the SingletonType from c++ and then import it in QML, this however doesn;t work either (for me).

    //In myQMLEngineLoader.cpp
    @void RegisterMySingleton()
    qmlRegisterSingletonType(QUrl::fromLocalFile("SomeSingleton.qml"), "Singletons.SomeSingleton", 1, 0, "MySingleton");

    This will not produce errors, but when referencing MySingleton in a QML doc (import Singletons.SomeSingleton 1.0 ), it will simply say Component not Ready and or that any of the fields are undefined. - note that making the URL incorrect on purpose also doesn't produce errors.

    I am at a loss as to how to approach styling/global data with QML. Stateless Libraries aren't really that nice, and instancing a "style" component for every component also seems stupid. It's especially disheartening when any info on styling with QML simply points to how nice Singletons are, while providing 0 docs on how to use them or how to register these (the info that does exist is obviously wrong).

    Please help,

  • Hi,

    I just investigated that myself yesterday. Here is what worked for me ...

    1. Use the pragma statement at the top of the QML file.

    pragma Singleton
    import QtQuick 2.0
    import QtQuick.LocalStorage 2.0

    QtObject {
    id: sqliteDb

    function getData(queryString)    {
        var db = LocalStorage.openDatabaseSync("fmDB", "1.0", "The Example QML SQL!", 1000000);
        var rs = null;
            function(tx) {
                // Show all added greetings
                rs = tx.executeSql(queryString);
        return rs;


    1. Place the qmldir file in the same directory as your qml files and put this line in it ...

    @singleton FMDataSingleton FMDataSingleton.qml@

    replace qml file name and singleton name to match your needs.

    1. place this import statement at the top of the file you are referencing your singleton from.

    @import "."@

    That worked for me!

  • Actually, it seems to work without the qmlRegisterSingletonType if u add in your qmldir

    Module SingletonFolder.Singletons
    singleton mySingleton 1.0 MySingleton.qml

    The addition of the singleton "keyword" seems to make it work, tho i am not sure if it's meant to work like this.

    Any clarification would be appreciated! :)

  • woop, u where to fast haha. Yeah i came to the same conclusion! Thanks for the info regardless! :)

  • The approach that is described here http://qt-project.org/forums/viewthread/34680 works for me, but I don't like that fact that I have to to use absolute path to qml file.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.