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. [RESOLVED] What would be the best way to tie an int value to an item in a QComboBox?

[RESOLVED] What would be the best way to tie an int value to an item in a QComboBox?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.2k Views
  • 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.
  • F Offline
    F Offline
    fs_tigre
    wrote on last edited by
    #1

    Hi,

    What would be the best way to tie an int value to an item in a QComboBox?

    In other words what I have is about 30 items in a QComboBox and each item is tied to an int number and I was wondering if I could somehow assign this int value when adding the item to the comboBox so that whenever that item is selected the value will be the int not the string (currentText()).

    This is what I could do but I think is overkill, too many if statements...

    @
    //add items
    comboBox->addItem("Item One");
    comboBox->addItem("Item Two");@

    @
    // check what item is selected and assign int value
    if(comboBox->currentText() == "Item One")
    {
    // assign intValue for item One

    }else if(comboBox->currentText() == "Item Two")
    {
        // assign intValue for item two
        
    }...@
    

    What I would like to do instead is assign a value when adding the items to the ComboBox
    @ // add items
    comboBox->addItem("Item One", 100);
    comboBox->addItem("Item Two", 200);@

    and than somehow ask for that value...
    @ // call for intValue of the selected item
    comboBox->currentValue(); // this would output the int value
    @

    Sorry if what I'm asking doesn't make too much sense.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bodzio131
      wrote on last edited by
      #2

      See that QComboBox::addItem has second argument named 'userData', so this:
      @comboBox->addItem("Item One", 100);@

      is perfectly valid, but then you need such thing:

      @comboBox->itemData( comboBox->currentIndex () );@

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fs_tigre
        wrote on last edited by
        #3

        Got it, I tried this but I was definitely doing it wrong.

        this is what I was doing...
        @comboBox->itemData->currentIndex ();@

        Thanks a lot

        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