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

How to use singleton in another singleton



  • Hello,

    I'm using Qt 5.3.2 and I tried to use singleton object in another singleton object in the same directory.
    But the program exited with code -1.

    It is a situation such as the following. (this is just an example)

    Directory structure
    @root [directory]
    |-Test.qml (Window)
    |-singletons [directory]
    |-qmldir
    |-Colors.qml (Singleton)
    |-Red.qml (Singleton)
    |-Green.qml (Singleton)@


    qmldir
    @singleton Colors Colors.qml
    singleton Red Red.qml
    singleton Green Green.qml@


    Colors.qml
    @pragma Singleton
    import QtQuick 2.0

    QtObject {
    readonly property color colorR: "#FF0000"
    readonly property color colorG: "#00FF00"
    readonly property color colorB: "#0000FF"
    }@


    Red.qml
    @pragma Singleton
    import QtQuick 2.0
    import '.'

    QtObject {
    readonly property color color1: Colors.colorR
    }@


    Green.qml
    @pragma Singleton
    import QtQuick 2.0
    import '.'

    QtObject {
    readonly property color color1: Colors.colorG
    }@


    Test.qml:
    @import QtQuick 2.3
    import QtQuick.Controls 1.2
    import 'singletons'

    ApplicationWindow {
    width: 640
    height: 480

    Rectangle {
        anchors.centerIn: parent
        width: 100
        height: 100
        color: Red.color1
    }
    

    }
    @


    Red.qml and Green.qml are using Colors.qml by importing '.'.
    I think this part is something wrong.

    The program works fine if I remove
    @import '.'@

    from Green.qml.
    But I will not be able to use Green.color1 in Test.qml.

    Please can anyone help me with this issue?

    Thanks.


Log in to reply