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. Best way to use string in QML loaded from JSON

Best way to use string in QML loaded from JSON

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 463 Views 2 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.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by
    #1

    I'm working with Qt5.9.2 under Linux. My application reads a JSON file that contains an array of strings and put them into a QStringList. I also have an enum to easily retrieve the desired one. Example:

    text.json

    {
    	"TEXTS": [
                "Hello World!",
                "blablabla"
    	]
    }
    

    myclass.cpp

    enum Texts
    {
        HelloWorld,
        BlaBlaBla
    };
    
    QStringList _texts;
    // load strings to _texts
    
    _texts[HelloWorld]; // access to a string
    

    Now I need to use those strings in QML pages instead of hardcoded ones:

    Text {
        color: "#f1f5f5"
        text: "Hello World!"
    }
    

    My first thought is to create a Q_INVOKABLE function to retrieve the string on the fly. Something like:

    Text {
        color: "#f1f5f5"
        text: loadString(MyNamespace.HelloWorld);
    }
    

    but it requires to expose an enum to QML and I'm not sure if it's the best way. Any other idea?

    raven-worxR 1 Reply Last reply
    0
    • M Mark81

      I'm working with Qt5.9.2 under Linux. My application reads a JSON file that contains an array of strings and put them into a QStringList. I also have an enum to easily retrieve the desired one. Example:

      text.json

      {
      	"TEXTS": [
                  "Hello World!",
                  "blablabla"
      	]
      }
      

      myclass.cpp

      enum Texts
      {
          HelloWorld,
          BlaBlaBla
      };
      
      QStringList _texts;
      // load strings to _texts
      
      _texts[HelloWorld]; // access to a string
      

      Now I need to use those strings in QML pages instead of hardcoded ones:

      Text {
          color: "#f1f5f5"
          text: "Hello World!"
      }
      

      My first thought is to create a Q_INVOKABLE function to retrieve the string on the fly. Something like:

      Text {
          color: "#f1f5f5"
          text: loadString(MyNamespace.HelloWorld);
      }
      

      but it requires to expose an enum to QML and I'm not sure if it's the best way. Any other idea?

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

      @Mark81 said in Best way to use string in QML loaded from JSON:

      but it requires to expose an enum to QML

      why does it require an enum? Only if you design it that way. You could also pass a string?

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

      • Login

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