Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Combobox doesnt display data if listelement contains more than 1 value
Qt 6.11 is out! See what's new in the release blog

Combobox doesnt display data if listelement contains more than 1 value

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 876 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.
  • C Offline
    C Offline
    chilarai
    wrote on last edited by
    #1

    I am following the example for Combobox from https://doc.qt.io/qt-5/qml-qtquick-controls-combobox.html.

    If the ListElement inside Listmodel contains more than 1 item, the Combobox displays a blank. Can someone point out if I am doing something wrong or if it is a bug? I have added comments in the sections of my code which works and which don't.

    import QtQuick 2.12
    import QtQuick.Controls 2.4
    import QtQuick.Layouts 1.3
    
    Page {
    
        id : somepageid
    
    
        // This works
    
        ComboBox {
            id : first
            width: 200
            model: [ "Banana", "Apple", "Coconut" ]
        }
    
    
        // This also works
    
        ComboBox {
            id : second
            anchors.top : first.bottom
            anchors.topMargin: 10
            currentIndex: 2
            model: ListModel {
                id: cbItems
                ListElement { text: "Banana" }
                ListElement { text: "Apple" }
                ListElement { text: "Coconut" }
            }
            width: 200
        }
    
    
        // This doesnt work
        // Need help here
    
        ComboBox {
            id : third
            anchors.top : second.bottom
            anchors.topMargin: 10
            currentIndex: 2
            model: ListModel {
                id: cbItems2
                ListElement { text: "Banana"; color: "Yellow" }
                ListElement { text: "Apple"; color: "Green" }
                ListElement { text: "Coconut"; color: "Brown" }
            }
            width: 200
    
        }
    }
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You need to tell combo box which value to display, like this:

      ComboBox {
        textRole: "color"
      }
      

      BTW. You have linked wrong doc. https://doc.qt.io/qt-5/qml-qtquick-controls2-combobox.html#combobox-model-roles

      (Z(:^

      C 1 Reply Last reply
      2
      • C Offline
        C Offline
        chilarai
        wrote on last edited by
        #3

        @sierdzio said in Combobox doesnt display data if listelement contains more than 1 value:

        textRole: "color"

        Thanks a lot. That solved my issue

        1 Reply Last reply
        0
        • sierdzioS sierdzio

          You need to tell combo box which value to display, like this:

          ComboBox {
            textRole: "color"
          }
          

          BTW. You have linked wrong doc. https://doc.qt.io/qt-5/qml-qtquick-controls2-combobox.html#combobox-model-roles

          C Offline
          C Offline
          chilarai
          wrote on last edited by
          #4
          This post is deleted!
          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