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. adding a color value to a ListElement
Forum Update on Monday, May 27th 2025

adding a color value to a ListElement

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 572 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 21 Apr 2021, 19:02 last edited by
    #1

    Hi all -

    I have and use a model as follows:

    ListModel {
      id: bottleModel_19
      ListElement {
      // position 1
      bottleColor: 0xff0000
      }
      
      ListElement {
      // position 2
      ...
    
    Repeater {
      id: bottleRepeater
      model: bottleModel_19
      Bottle {
        cellColor: bottleColor
        ...
    

    But the desired colors aren't displaying (I just get black, which is overriding the default value in the Bottle object).

    For brevity, I've edited out several other items in the model, all of which work, so I know the model is being read. Is there something about colors that need special handling? I've tried defining the values as ints and as strings...no luck.

    Thanks...

    O 1 Reply Last reply 21 Apr 2021, 20:35
    0
    • M mzimmers
      21 Apr 2021, 19:02

      Hi all -

      I have and use a model as follows:

      ListModel {
        id: bottleModel_19
        ListElement {
        // position 1
        bottleColor: 0xff0000
        }
        
        ListElement {
        // position 2
        ...
      
      Repeater {
        id: bottleRepeater
        model: bottleModel_19
        Bottle {
          cellColor: bottleColor
          ...
      

      But the desired colors aren't displaying (I just get black, which is overriding the default value in the Bottle object).

      For brevity, I've edited out several other items in the model, all of which work, so I know the model is being read. Is there something about colors that need special handling? I've tried defining the values as ints and as strings...no luck.

      Thanks...

      O Offline
      O Offline
      oria66
      wrote on 21 Apr 2021, 20:35 last edited by
      #2

      @mzimmers Hi. The only way I can get it to work is with strings. For example:

      The model:

      ListModel{
            id: elementModel
            ListElement{ theText:qsTr("Geofence"); theColor: "#FA6800"; active: true}
            ListElement{ theText:qsTr("Fields"); theColor: "aqua"; active: true}
            ListElement{ theText:qsTr("Grids"); theColor: "gray"; active: true}
            ListElement{ theText:qsTr("Machines");  theColor: "orange"; active: true}
            ListElement{ theText:qsTr("Interest points");  theColor: "magenta"; active: true}
            ListElement{ theText:qsTr("Sprinkles"); theColor: "white"; active: true}
            ListElement{ theText:qsTr("People"); theColor: "red"; active: true}
            ListElement{ theText:qsTr("Design"); theColor: "gray"; active: false}
            ListElement{ theText:qsTr("Pattern"); theColor: "gray"; active: false}
      }
      

      The delegate extract:

      ItemDelegate{
      ...
          RowLayout{
              spacing: 10
              anchors.fill: parent
              Label{ id: labelButton; text: "\uE09D"; color: model.theColor; font.family: "fontello"}
              Label{ id: label; Layout.fillWidth: true; text: model.theText ;font.pointSize: 10}
          }
      ...
      }
      

      Result:

      Kazam_screenshot_00013.png

      The truth is out there

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mzimmers
        wrote on 21 Apr 2021, 21:39 last edited by mzimmers
        #3

        Right you are (I'm not sure what I did to make it work, but it's working now). Thanks!

        So, for extra credit...here's what I'd like to do:

          property string colorForWash: "#373a36"
          property string colorForAGTP: "#be3a34"
          property string colorForA: "#ffc658"
          property string colorForP: "#26d07c"
          property string colorForS: "#005eb8"
        
          ListModel {
            id: bottleModel_19
            property int rackWidth: 515
            ListElement {
              // position 1
              x: 407
              y: 18
              height: 78
              width: 78
              bottleLabel: "ETH"
              bottleColor: colorForWash
            }
        

        This crashes...something to do with my use of "colorForWash." Can you see anything wrong with my syntax? The editor's not giving me any warning.

        EDIT:

        Oh, I think I see the problem...as someone reminded me recently, ListElements don't have properties, only roles. Hmm...not sure how to work around this one...

        O 1 Reply Last reply 22 Apr 2021, 01:51
        0
        • M mzimmers
          21 Apr 2021, 21:39

          Right you are (I'm not sure what I did to make it work, but it's working now). Thanks!

          So, for extra credit...here's what I'd like to do:

            property string colorForWash: "#373a36"
            property string colorForAGTP: "#be3a34"
            property string colorForA: "#ffc658"
            property string colorForP: "#26d07c"
            property string colorForS: "#005eb8"
          
            ListModel {
              id: bottleModel_19
              property int rackWidth: 515
              ListElement {
                // position 1
                x: 407
                y: 18
                height: 78
                width: 78
                bottleLabel: "ETH"
                bottleColor: colorForWash
              }
          

          This crashes...something to do with my use of "colorForWash." Can you see anything wrong with my syntax? The editor's not giving me any warning.

          EDIT:

          Oh, I think I see the problem...as someone reminded me recently, ListElements don't have properties, only roles. Hmm...not sure how to work around this one...

          O Offline
          O Offline
          oria66
          wrote on 22 Apr 2021, 01:51 last edited by oria66
          #4

          @mzimmers Right, ListElements are a collection of roles. If you want to set "dynamically" those roles, you should play with the javascript functions of ListModel, and implement some kind of mechanism (depends on your use case) to update it.

          The truth is out there

          1 Reply Last reply
          0

          1/4

          21 Apr 2021, 19:02

          • Login

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