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. QMap access in QML

QMap access in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 1 Posters 1.9k 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.
  • J Offline
    J Offline
    jars121
    wrote on last edited by
    #1

    I have 10 properties, each defined in a c++ class as follows:

    Q_PROPERTY(float titleWidget1 READ titleWidget1 NOTIFY titleWidgetChanged1)
    float titleWidget1() const{return m_titleWidget1;}
    

    The class is exposed to my QML, which currently works nicely, such that I can update QML text with myClass.titleWidget1. This approach doesn't scale particularly well, as I'm having to do a lot of if (selectedWidget == 1) statements to set the QML text to the correct widget number.

    As such, I'd like to replace the above implementation with a QMap, so I can have the following functionality:

    map<int, string> widgetTitle;
        for (int i(1); i < 11; i++) {    
    	widgetTitle[i] = "widgetTitle" + i;
    

    Then in QML, I'd be able to assign text as follows:

    text: myClass.widgetTitle[selectedWidget]
    

    Where selectedWidget is an exposed property I use to store the map index to be used in QML.

    Is the above a logical approach? QML will need to be able to update on changes to the map, so I don't believe a QStringList, QVariantList or QObjectList are suitable. Could the above functionality be provided with a QAbstractItemModel, or is there a way I can use a QMap?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jars121
      wrote on last edited by
      #2

      To make it a little clearer, imagine I've got multiple QProperties for each of the 10 hypothetical widgets:

      widgetSizeX1
      widgetSizeY1
      widgetColour1
      widgetTitle1
      //etc
      

      If each of the above were contained in a QMap, so that I could read/set the values for each of the 10 widgets as follows:

      map<int, int> widgetSizeX
          for (int i(1); i < 11; i++) {
             widgetSizeX[i] = i
             //This would make Widget1 1px wide and Widget10 10px wide (for example).
          }
      

      Then in QML, if Widget 4 was selected, such that selectedWidget == 4, I could use the map values from c++ to set properties of the relevant QML item:

      width: myClass.widgetSizeX[selectedWidget]
      height: myClass.widgetSizeY[selectedWidget]
      //etc.
      
      1 Reply Last reply
      0

      • Login

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