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. Robust way to map QComboBox items to internal types
Forum Updated to NodeBB v4.3 + New Features

Robust way to map QComboBox items to internal types

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.3k 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.
  • J Offline
    J Offline
    Justin Sayne
    wrote on last edited by
    #1

    I'd like to fill a QComboBox with various types and map them to different logic in my application. What's the most robust way to do this?

    I could do string comparison:

    if(box->currentText() == "Item 1")
      do this;
    else if(box->currentText() == "Item 2")
      do that;
    

    but that breaks as soon as I rename my items and - worst of all - I might not even notice I've broken sth.

    Another solution is to do an index mapping:

    if(box->currentIndex() == 1)
      do this;
    else if(box->currentIndex() == 2)
      do that;
    

    or even

    enum ComboBoxTypes { Item_1, Item_2 }
    if(box->currentIndex() == Item)
      do this;
    else if(box->currentIndex() == Item)
      do that;
    

    but this again breaks without notice if I reorder, change or delete items.
    Is there a better way to do this?

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      There's setItemData() for exactly this purpose. You can associate any kind of data with the items and then add/reorder/delete them and the data stays and can be queried with itemData() or currentData()
      For example:

      enum ComboBoxTypes { Foo, Bar }
      box->setItemData(0, Foo);
      box->setItemData(1, Bar);
      
      //and then
      
      if (box->currentData().toInt() == Foo)
          //do stuff...
      

      If you need to associate more then one piece of data you can do that by specifying the extra param, e.g.

      box->setItemData(0, someStuff, Qt::UserRole);
      box->setItemData(0, someOtherStuff, Qt::UserRole + 1);
      

      and same for retrieval.

      It's also common to put a pointer to some data structure of yours as the item data. This way you get an instant access to data related to the item.

      1 Reply Last reply
      4
      • J Offline
        J Offline
        Justin Sayne
        wrote on last edited by
        #3

        This is a nice solution but it only works in code, right? There is not a way to do this in QtDesigner by adding a QVariant to each item?!

        1 Reply Last reply
        0
        • Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          No, you can't set this up in the designer.

          1 Reply Last reply
          1

          • Login

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