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. QComboBox for Equally named Items
Forum Updated to NodeBB v4.3 + New Features

QComboBox for Equally named Items

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 642 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.
  • ModelTechM Offline
    ModelTechM Offline
    ModelTech
    wrote on last edited by ModelTech
    #1

    I have a collection of named items. It is allowed to have multiple items with the same name. I need a QComboBox feature to select an item from the set, which I currently setup by using the names of the items as text values for the selection options. How can I create a QComboBox that may contain multiple times the same value of displayed text for the selection option, while still being able to find the reference back to the appropriate item? Is there some variant of a QComboBox that works with something like a QComboBoxItem?

    By the way, I use alphabetic ordering...

    aha_1980A 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Atleast I'm not very clear about the question. Are you asking something like follows ?

      QStringList list;
      list << "A" << "A" << "B" <<"B";
      QComboBox box;
      box.addItems(list);
      

      ``

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      3
      • ModelTechM ModelTech

        I have a collection of named items. It is allowed to have multiple items with the same name. I need a QComboBox feature to select an item from the set, which I currently setup by using the names of the items as text values for the selection options. How can I create a QComboBox that may contain multiple times the same value of displayed text for the selection option, while still being able to find the reference back to the appropriate item? Is there some variant of a QComboBox that works with something like a QComboBoxItem?

        By the way, I use alphabetic ordering...

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi @ModelTech,

        you can add a const QVariant &userData to each item when adding: http://doc.qt.io/qt-5/qcombobox.html#addItem and later get back with http://doc.qt.io/qt-5/qcombobox.html#itemData for a specific index.

        Does that help you?

        Qt has to stay free or it will die.

        1 Reply Last reply
        4
        • ModelTechM Offline
          ModelTechM Offline
          ModelTech
          wrote on last edited by
          #4

          Thanks for the answers!

          @aha_1980 If the const QVariant &userData can be a pointer to my item (which is subclassed from QObject and uses the Q_OBJECT macro), than this solves what I am looking for. In the QVariant documentation, I read that: "If the QVariant contains a pointer to a type derived from QObject then T may be any QObject type. If the pointer stored in the QVariant can be qobject_cast to T, then that result is returned. Otherwise a null pointer is returned. Note that this only works for QObject subclasses which use the Q_OBJECT macro." So, this is the way to go.

          Unfortunately, I have not used QVariant before. So, I have been reading more about it but could not find example code for this particular situation. Would anybody have example code for a QVariant for such a pointer? I can only imagine something like:

          MySubClassedQObjectClass *MyObject, *MyOtherObject;
          QVariant Variant = QVariant::fromValue(MyObject);
          MyOtherObject = Variant.value<MySubClassedQObjectClass*>()
          
          1 Reply Last reply
          0
          • ModelTechM Offline
            ModelTechM Offline
            ModelTech
            wrote on last edited by ModelTech
            #5

            Well, the code I came up with seems to work.

            I think however that I need to store a QMap from the MySubClassedQObjectClass items to the corresponding QVariants in order to be able to call QComboBox::fromData but that is fine. Unless there is a better way for this. Anyway, I think this is a good solution!

            The only thing that does not work anymore is the alphabetic insertion into the QComboBox that I realized using QComboBox::setInsertPolicy(QComboBox::InsertAlphabetically). Is this a bug?

            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