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. assign value from combobox to string
Forum Updated to NodeBB v4.3 + New Features

assign value from combobox to string

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 448 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.
  • M Offline
    M Offline
    MScottM
    wrote on last edited by
    #1

    This seems like it should be easy, but I can't find the right solution. I want to assign a value selected in a combobox to a string that gets used by a function.

    Here is my code:

    property string relayString: ""
    
    Shared.Button {
                        id: pingRelay                   
                        text: qsTr("    Ping Relay    ")
                        onClicked: {
                            relayString = cboRelays.currentValue // <- error is here
                            console.log("selected Relay: " + relayString) // troubleshooting
                            id = "01A00640"
                            qmlData = relayString
                            pingBoard(id, qmlData)
                        }
                    }
    
                    ComboBox {
                        id: cboRelays                   
                        textRole: "key"
                        valueRole: "string"
                        model:  ListModel {
                            ListElement { key: "Relay 1"; value: "1" }
                            ListElement { key: "Relay 2"; value: "2" }
                            ListElement { key: "Relay 3"; value: "3" }
                        }
                    }
    
    

    When I click the button I get an error message: "Cannot assign [undefined] to QString".

    posktomtenP 1 Reply Last reply
    0
    • M MScottM

      @posktomten - thanks for the reply. Unfortunately that gives me the 'key' value ("Relay x"). What I want is the 'value' that goes with the key. So if 'Relay 2' is selected, it gives the string "2".

      Hope that makes sense...

      B Offline
      B Offline
      Bonnie
      wrote on last edited by
      #4

      @MScottM I'm not familiar with QML, but I think since you set valueRole to be "string", then it should be

      ListElement { key: "Relay 1"; string: "1" }
      

      not value:...

      M 1 Reply Last reply
      1
      • M MScottM

        This seems like it should be easy, but I can't find the right solution. I want to assign a value selected in a combobox to a string that gets used by a function.

        Here is my code:

        property string relayString: ""
        
        Shared.Button {
                            id: pingRelay                   
                            text: qsTr("    Ping Relay    ")
                            onClicked: {
                                relayString = cboRelays.currentValue // <- error is here
                                console.log("selected Relay: " + relayString) // troubleshooting
                                id = "01A00640"
                                qmlData = relayString
                                pingBoard(id, qmlData)
                            }
                        }
        
                        ComboBox {
                            id: cboRelays                   
                            textRole: "key"
                            valueRole: "string"
                            model:  ListModel {
                                ListElement { key: "Relay 1"; value: "1" }
                                ListElement { key: "Relay 2"; value: "2" }
                                ListElement { key: "Relay 3"; value: "3" }
                            }
                        }
        
        

        When I click the button I get an error message: "Cannot assign [undefined] to QString".

        posktomtenP Offline
        posktomtenP Offline
        posktomten
        wrote on last edited by
        #2

        @MScottM

        QComboBox comboBox;
        QString str = comboBox.currentText();
        

        Should work...

        posktomten

        M 1 Reply Last reply
        0
        • posktomtenP posktomten

          @MScottM

          QComboBox comboBox;
          QString str = comboBox.currentText();
          

          Should work...

          M Offline
          M Offline
          MScottM
          wrote on last edited by
          #3

          @posktomten - thanks for the reply. Unfortunately that gives me the 'key' value ("Relay x"). What I want is the 'value' that goes with the key. So if 'Relay 2' is selected, it gives the string "2".

          Hope that makes sense...

          B posktomtenP 2 Replies Last reply
          0
          • M MScottM

            @posktomten - thanks for the reply. Unfortunately that gives me the 'key' value ("Relay x"). What I want is the 'value' that goes with the key. So if 'Relay 2' is selected, it gives the string "2".

            Hope that makes sense...

            B Offline
            B Offline
            Bonnie
            wrote on last edited by
            #4

            @MScottM I'm not familiar with QML, but I think since you set valueRole to be "string", then it should be

            ListElement { key: "Relay 1"; string: "1" }
            

            not value:...

            M 1 Reply Last reply
            1
            • M MScottM

              @posktomten - thanks for the reply. Unfortunately that gives me the 'key' value ("Relay x"). What I want is the 'value' that goes with the key. So if 'Relay 2' is selected, it gives the string "2".

              Hope that makes sense...

              posktomtenP Offline
              posktomtenP Offline
              posktomten
              wrote on last edited by
              #5

              @MScottM
              Yes, stupid of me!
              Come to think of QMap. (I do not master QML)

              posktomten

              1 Reply Last reply
              0
              • B Bonnie

                @MScottM I'm not familiar with QML, but I think since you set valueRole to be "string", then it should be

                ListElement { key: "Relay 1"; string: "1" }
                

                not value:...

                M Offline
                M Offline
                MScottM
                wrote on last edited by
                #6

                @Bonnie

                Yes! that was it.

                Thank you both!

                1 Reply Last reply
                0
                • M MScottM has marked this topic as solved on

                • Login

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