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
Forum Updated to NodeBB v4.3 + New Features

Use Delegate in ComboBox

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 312 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 1 Apr 2022, 19:39 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
                        }
                     }
                    }
                
    
    T 1 Reply Last reply 2 Apr 2022, 19:12
    0
    • C chiyuwang
      1 Apr 2022, 19:39

      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
                          }
                       }
                      }
                  
      
      T Offline
      T Offline
      Tassos
      wrote on 2 Apr 2022, 19:12 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

      1/2

      1 Apr 2022, 19:39

      • Login

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