Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Unable to define a list of values shared across multiple QML files using a Singleton
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 546 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JL SABATIER
    wrote on last edited by
    #1

    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

    DiracsbracketD raven-worxR 2 Replies Last reply
    0
    • J JL SABATIER

      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

      DiracsbracketD Offline
      DiracsbracketD Offline
      Diracsbracket
      wrote on last edited by
      #2

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

      1 Reply Last reply
      1
      • J JL SABATIER

        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

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @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)

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        1
        • J Offline
          J Offline
          JL SABATIER
          wrote on last edited by
          #4

          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 ..

          raven-worxR 1 Reply Last reply
          0
          • J JL SABATIER

            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 ..

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @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

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved