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. How do you specify values for a ComboBox?
Forum Updated to NodeBB v4.3 + New Features

How do you specify values for a ComboBox?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 4 Posters 503 Views 2 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.
  • K Offline
    K Offline
    kitfox
    wrote on last edited by
    #1

    I have a combo box that looks like

                    ComboBox {
                        id: gridCombo
    
                        model: ListModel {
                            id: gridModel
                            ListElement {
                                text: "1/1"
                            }
                            ListElement {
                                text: "1/2"
                            }
                            ListElement {
                                text: "1/4"
                            }
                            ListElement {
                                text: "1/8"
                            }
                        }
                    }
    

    I would like to associate integers with each label equal to the denominator of each label. Is there a way to do this? If I try adding a 'value' field, the combo box displays blank entries.

    I've also tried this alternate syntax, but it also displays blanks:

                    ComboBox {
                        id: gridCombo
                        model: [
                            { text: firstValue, value: firstValue },
                            { text: secondValue, value: secondValue },
                            { text: thirdValue, value: thirdValue }
                        ]
                    }
    
    jeremy_kJ 1 Reply Last reply
    0
    • K kitfox

      Yes I have. It didn't help. Like I said in my post, the second form isn't displaying anything.

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #5

      @kitfox said in How do you specify values for a ComboBox?:

      Yes I have. It didn't help. Like I said in my post, the second form isn't displaying anything.

      Then you don't have read it carefully, you have to define textRole and valueRole, cf https://doc.qt.io/qt-5/qml-qtquick-controls2-combobox.html#combobox-model-roles:

      ComboBox {
          textRole: "text"
          valueRole: "value"
          id: gridCombo
          model: [
              { text: firstValue, value: firstValue },
              { text: secondValue, value: secondValue },
              { text: thirdValue, value: thirdValue }
          ]
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      4
      • MarkkyboyM Offline
        MarkkyboyM Offline
        Markkyboy
        wrote on last edited by
        #2

        Have you read the documentation for ComboBox QML?; https://doc.qt.io/qt-5/qml-qtquick-controls2-combobox.html

        Don't just sit there standing around, pick up a shovel and sweep up!

        I live by the sea, not in it.

        1 Reply Last reply
        2
        • K Offline
          K Offline
          kitfox
          wrote on last edited by kitfox
          #3

          Yes I have. It didn't help. Like I said in my post, the second form isn't displaying anything.

          KroMignonK 1 Reply Last reply
          0
          • K kitfox

            I have a combo box that looks like

                            ComboBox {
                                id: gridCombo
            
                                model: ListModel {
                                    id: gridModel
                                    ListElement {
                                        text: "1/1"
                                    }
                                    ListElement {
                                        text: "1/2"
                                    }
                                    ListElement {
                                        text: "1/4"
                                    }
                                    ListElement {
                                        text: "1/8"
                                    }
                                }
                            }
            

            I would like to associate integers with each label equal to the denominator of each label. Is there a way to do this? If I try adding a 'value' field, the combo box displays blank entries.

            I've also tried this alternate syntax, but it also displays blanks:

                            ComboBox {
                                id: gridCombo
                                model: [
                                    { text: firstValue, value: firstValue },
                                    { text: secondValue, value: secondValue },
                                    { text: thirdValue, value: thirdValue }
                                ]
                            }
            
            jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by
            #4

            @kitfox said in How do you specify values for a ComboBox?:

            I've also tried this alternate syntax, but it also displays blanks:

                            ComboBox {
                                id: gridCombo
                                model: [
                                    { text: firstValue, value: firstValue },
                                    { text: secondValue, value: secondValue },
                                    { text: thirdValue, value: thirdValue }
                                ]
                            }
            

            According to the documentation:

            When using models that have multiple named roles, ComboBox must be configured to use a specific text role for its display text and delegate instances. If you want to use a role of the model item that corresponds to the text role, set valueRole. The currentValue property and indexOfValue() method can then be used to get information about those values.

            Unless textRole and valueRole are set, the ComboBox doesn't know which role to use for each purpose.

            Asking a question about code? http://eel.is/iso-c++/testcase/

            1 Reply Last reply
            1
            • K kitfox

              Yes I have. It didn't help. Like I said in my post, the second form isn't displaying anything.

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #5

              @kitfox said in How do you specify values for a ComboBox?:

              Yes I have. It didn't help. Like I said in my post, the second form isn't displaying anything.

              Then you don't have read it carefully, you have to define textRole and valueRole, cf https://doc.qt.io/qt-5/qml-qtquick-controls2-combobox.html#combobox-model-roles:

              ComboBox {
                  textRole: "text"
                  valueRole: "value"
                  id: gridCombo
                  model: [
                      { text: firstValue, value: firstValue },
                      { text: secondValue, value: secondValue },
                      { text: thirdValue, value: thirdValue }
                  ]
              }
              

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              4

              • Login

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