Solved Combobox doesnt display data if listelement contains more than 1 value
-
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 } }
-
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
-
@sierdzio said in Combobox doesnt display data if listelement contains more than 1 value:
textRole: "color"
Thanks a lot. That solved my issue
-
This post is deleted!