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. Dynamically reference QML components
Forum Updated to NodeBB v4.3 + New Features

Dynamically reference QML components

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 231 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.
  • R Offline
    R Offline
    respect88
    wrote on last edited by respect88
    #1

    I would like to use the string returned from key.event.text to dynamically reference the component that has a matching id.

    I could have many different components with id's that I don't know of which is why I am trying to make this dynamic, hence the commented out switch statement.

    import QtQuick 2.15
    import QtQuick.Window 2.15
    
    Window {
        width: 300
        height: 300
        visible: true
    
        Item {
            focus: true
            Keys.onPressed: {
                rect_id = event.text
                rect_id.color = 'red'
    
                // switch (event.text) {
                //     case 'q':
                //         q.color = 'red'
                //         break;
                //     case 'w':
                //         w.color = 'red'
                //         break;
                // }
            }
        }
        Row {
            Rectangle {
                id: 'q'
                width: 50
                height: 50
                border.color: '#000'
                border.width: 1
                color: 'transparent'
            }
    
            Rectangle {
                id: 'w'
                width: 50
                height: 50
                border.color: '#000'
                border.width: 1
                color: 'transparent'
            }
        }
    
    }
    
    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2
      property var dict = ({
        'q' : q
        'w' : w
      })
      
      Item {
        id: q
        function func1(){}
      }
      Item {
        id: w
        function func1(){}
      }
      

      Access them:

      dict['w'].func1()
      

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      1
      • R Offline
        R Offline
        respect88
        wrote on last edited by
        #3

        Thanks! Works

        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