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. Generate JS object with embedded context property generated from text?
Forum Update on Monday, May 27th 2025

Generate JS object with embedded context property generated from text?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 1 Posters 168 Views
  • 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.
  • F Offline
    F Offline
    fcarney
    wrote on 15 Apr 2022, 17:53 last edited by
    #1

    I have a set of context properties defined by another object. They are contiguous in their naming as xmBtn1 ... xmBtn6. I want to handle these in QML in a similar manner. How do I go about generating an object with a reference to these context properties without explicitly making a list of them: property var objs: [xmBtn1, xmBtn2, ... xmBtn6]?

    I tried to generate a JSON object by parsing, but the object lacks context and fails to generate:

                        let buttons = []
                        for(let ind=0; ind<6; ++ind){
                            let str = "{button: %1}".arg("xmBtn%1".arg(ind+1))
                            console.log(str)
                            buttons.append(JSON.parse(str))
                        }
    

    Output:

    qml: {button: xmBtn1}
    SyntaxError: JSON.parse: Parse error
    

    Pretty sure JSON.parse doesn't know what to do with xmBtn1 as that is not in the context of the parse.

    Is there a function in QML I can call that can retrieve a valid object to a symbol that is defined in the context?
    Something like: Qt.getproperty("propertyname") ?

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fcarney
      wrote on 15 Apr 2022, 18:45 last edited by
      #2

      Did I just find the one legitimate use for eval?

      let buttons = []
      for(let ind=0; ind<6; ++ind){
          let obj = {button: eval("xmBtn%1".arg(ind+1))}
          console.log(JSON.stringify(obj))
          buttons.push(obj)
      }
      statusButtons = buttons
      

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      1

      2/2

      15 Apr 2022, 18:45

      • Login

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