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

Unable to define a list of values shared across multiple QML files using a Singleton



  • Good Morning to you all,

    I'm unable to define a list of values shared across multiple QML files though I can't see where my fault is.
    My (attempted) solution is the following :

    1. I'm defining a Singleton in a file named "EnumMessages.qml" which contains the following code:

      pragma Singleton
      import QtQuick 2.5
      QtObject {
      id: singleton

       [... snipped elements of the list ...]
      
       property int _LNG_MSG_TITLE_SETTINGS : 18 
      
       [... snipped elements of the list ...]
      

      }

    2. I'm trying to use it in a few other files.. Like this file "PageSetings.qml", that contains the following code:

      import QtQuick 2.5;

      import ".";

      AbstractPage {
      id: _self;

           [... snipped some code ...]
      
           console.log("_LNG_MSG_TITLE_SETTINGS=", EnumMessages._LNG_MSG_TITLE_SETTINGS ) ;
      

    The console.log above displays the following message :

    qml: _LNG_MSG_TITLE_SETTINGS= undefined
    

    Someone could please tell me what I'm getting wrong??

    Thanks in advance for your help,

    JLS



  • @JL-SABATIER Just to check, did you register the singleton with qmlRegisterSingletonType ?


  • Moderators

    @JL-SABATIER
    to define a singleton with a QML file you also need to provide a qmldir file with a corresponding singleton line (when you import "." like you do in your snippet). For a cusstom import url see this

    But when you are just after defining "static" int values you can also use enums in QML (since Qt 5.10 IIRC)



  • Thank you very much for your help. That fixed my problems.

    I may use enum values instead of that singleton. But I need to share these definitions among all QML pages, and to do so, I guess I'll have to declare this enum on the model side, in C++, where it is not needed ..


  • Moderators

    @JL-SABATIER said in Unable to define a list of values shared across multiple QML files using a Singleton:

    I may use enum values instead of that singleton. But I need to share these definitions among all QML pages, and to do so, I guess I'll have to declare this enum on the model side, in C++,

    no, just import the url the QML file with the enum is in


Log in to reply