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. Use Delegate in ComboBox
Qt 6.11 is out! See what's new in the release blog

Use Delegate in ComboBox

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 505 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
    chiyuwang
    wrote on last edited by
    #1

    Hi, I very new in Qt, now I need to use delegate to populate items in a ComboBox, I can find that Text can be used to assign text of a item, but I do not know how to apply item's value. it is like only textRole works, but I do not know how to make valueRole work in delegate.

    My code look like this, it loads items for now.

                    ComboBox {
                        implicitWidth: 150
                        editable: false
                        ToolTip.text: qsTr('Select Item')
                        ToolTip.visible: hovered
                        model: itemsModel
                        currentIndex: indexOfValue(getPropertyInt("items"))
                        delegate:ItemDelegate{
                            required property var properties
                            id: itemInfo
                            text: itemInfo.properties.name
                        }
                     }
                    }
                
    
    TassosT 1 Reply Last reply
    0
    • C chiyuwang

      Hi, I very new in Qt, now I need to use delegate to populate items in a ComboBox, I can find that Text can be used to assign text of a item, but I do not know how to apply item's value. it is like only textRole works, but I do not know how to make valueRole work in delegate.

      My code look like this, it loads items for now.

                      ComboBox {
                          implicitWidth: 150
                          editable: false
                          ToolTip.text: qsTr('Select Item')
                          ToolTip.visible: hovered
                          model: itemsModel
                          currentIndex: indexOfValue(getPropertyInt("items"))
                          delegate:ItemDelegate{
                              required property var properties
                              id: itemInfo
                              text: itemInfo.properties.name
                          }
                       }
                      }
                  
      
      TassosT Offline
      TassosT Offline
      Tassos
      wrote on last edited by
      #2

      @chiyuwang Your model should provide a role name for the desired value, let's say value, then you can inform the combobox via its valueRole property.
      See the example below, copied from Qt documentation

      ApplicationWindow {
          width: 640
          height: 480
          visible: true
      
          // Used as an example of a backend - this would usually be
          // e.g. a C++ type exposed to QML.
          QtObject {
              id: backend
              property int modifier
          }
      
          ComboBox {
              textRole: "text"
              valueRole: "value"
              // When an item is selected, update the backend.
              onActivated: backend.modifier = currentValue
              // Set the initial currentIndex to the value stored in the backend.
              Component.onCompleted: currentIndex = indexOfValue(backend.modifier)
              model: [
                  { value: Qt.NoModifier, text: qsTr("No modifier") },
                  { value: Qt.ShiftModifier, text: qsTr("Shift") },
                  { value: Qt.ControlModifier, text: qsTr("Control") }
              ]
          }
      }
      
      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