Important: Please read the Qt Code of Conduct -

Singleton Additional Property Issue

  • I am using Singleton file to have all the properties in a single file. So I have around 50+ properties in Styles.qml and it works.

    Whenever I keep adding new properties in Styles.qml I am getting some unusual issue .When I start using these properties I am getting "Unable to assign property"

    The code follows below :


    pragma Singleton
    import QtQuick 2.0


    //Login Dialog
    property string temp: "temp"
    property color mainwindowColor: "#cccccc"
    property color transparentColor: "transparent"
    property color whiteColor: "white"
    property color titleTextColor: "#00385F"


    To make it Singleton I have used qmldir :
    singleton Styles 1.0 Styles.qml

  • Moderators

    you say it works but still got an issue?
    What does working mean in this case then?

  • @raven-worx Yes It is working but when I add new properties it starts giving some unusual errors

  • HI @Adithya
    What you mean by some unusual errors ?
    Can you provide clear details ?

  • @Pradeep-P-N The old properties will stop working whenever I add new properties , I mean it will start showing errors such unable to assign color (This properties where working fine before but when I add new properties . It will start giving these errors)

  • Hi @Adithya
    It should work fine, independent of adding properties.

    Just check is you are following below sample

    • main.cpp :
      qmlRegisterSingletonType(QUrl(QSL("qrc:///qml/MyStyle.qml")), "AppStyles", 1, 0, "MyStyle");

    • MyStyle.qml replace Item {} with QtObject {}

         pragma Singleton
         import QtQuick 2.8
         // import if you need any other 
         QtObject {
             // Add your own style properties here
             // eg: readonly property bool on: true
             // To use the style: import AppStyles 1.0
             // Then: MyStyle.propertyName in other QML files
              readonly property string temp: "temp"
              readonly property color mainwindowColor: "#cccccc"
               - - - - - - 
               - - - - - - 
    • Prefer using readonly property in the Style.qml

    All the best.

  • @Adithya

    Please if your issue is resolved, make it as SOLVED.

Log in to reply