17

I'm using Qt Designer (well, Qt Creator actually, but specifically the part derived from Qt Designer), and I've added a few QComboBox items to a dialog with a constant list of items. I need to map the items in the combo box to strings (which are distinct from the displayed strings). The best idea I've come up for it is to use the QComboBox::itemData function to get the needed string from the selected item, but I'm having trouble adding the associated strings to the items. I've looked all over the designer and have not yet seen a way to add the user data. Is there one there that I'm missing? I'm also willing to directly edit the XML of the .ui file to add the property if needed, but I can't figure out what the property name would be. Is there one that I can use here? Currently I'm adding the data in code, but it doesn't seem like the correct solution to me.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
LRFLEW
  • 1,251
  • 3
  • 11
  • 19
  • It's seems unlikely that this would be implemented in Qt Designer, given that the item data is a QVariant. And I don't see that it's wrong or unusual to populate the combo in code. – ekhumoro Feb 02 '16 at 01:03
  • I'm not sure about that. Pretty much any property is a QVariant, with the type selection determining the value type within the QVariant. This is how it works with dynamic properties, such as if you add a property in the designer and access it with QObject::property. As to why I don't want to put it in a code file, it seems like bad practice to separate the item instantiation and the data instantiation as it makes adding/editing the items more cumbersome. – LRFLEW Feb 02 '16 at 02:17
  • The data has to be serialized as xml, which is why I am suggesting it is unlikely to be supported (but not impossible in principle). I'm afraid I don't understand why you think populating a combo box in code is "bad practice" or cumbersome. – ekhumoro Feb 02 '16 at 02:53
  • I'm *not* populating the combo box in code. The items of the combo box are being setup in the .ui file. It's the data that's associated with it that is being setup in code. So the items and their text are in the .ui file and the data is in a separate source file. – LRFLEW Feb 02 '16 at 03:06
  • Ah - well I was actually suggesting you do populate both text *and* data in code. I agree that populating them separately is not a good solution. – ekhumoro Feb 02 '16 at 03:56

5 Answers5

36

Edit: Because this answer seems to be getting more upvotes than it should. I'll leave it here, because a lot of people seem to find this answer when they google for their problem. The answer below is much better suited to OP's question.

In QT designer 5.6.2 you can double-click the combo-box to add items.

Dschoni
  • 3,714
  • 6
  • 45
  • 80
  • Thanks, this answer was useful and it is more up to date. – Isma Aug 19 '18 at 12:17
  • 9
    This answers the wrong question. Yes, you can add more elements to a QComboBox in this way, but that's not what i was asking about. I was asking about setting the item data for an already-added item. http://doc.qt.io/qt-5/qcombobox.html#setItemData Setting your own Qt::UserRole data is not supported in Qt Designer. You can only edit the text and icon data for each element. – LRFLEW Aug 24 '18 at 08:54
  • It's about adding a QVariant data to each items and not about adding items. If we need to add data manually, then it is better to add items too. – Keshav Sahu Apr 07 '22 at 11:56
  • 1
    @KeshavSahu That is fully understood and covered by the italic disclaimer in my answer. – Dschoni May 05 '22 at 10:42
  • 1
    This is the answer I was looking for. I am not OP, though :D – Dženan Mar 23 '23 at 20:45
15

Ok, so I actually went through the source code of the uic and found the spot that handles QComboBox. As of the current version of Qt (so 5.5.1), there is no support for setting the data attribute of the items through the .ui files. I may make this a feature request, but for now, it's unsupported.

LRFLEW
  • 1,251
  • 3
  • 11
  • 19
3

This is how to store data, in addition to the text, in each ComboBox Item.

item_text = 'This is my text'
item_data = []
your_comboBox.addItem(item_text, item_data)

To retrieve the data:

item_index = 0
y_data = your_comboBox.itemData(item_index)
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Matheus Torquato
  • 1,293
  • 18
  • 25
1

It is not supported and unfortunately it is very unlikely that it will be ever implemented. See: https://bugreports.qt.io/browse/QTBUG-50823

"This out of scope for a UI design tool."

  • The irony is that the bug report you linked was the one _I_ opened a little after asking this question. I never posted it in this thread, so thank you for doing that. – LRFLEW Aug 05 '22 at 06:47
-1

Add items during runtime:

ui.ComboBox.addItem('My New Combo Box Item')

Map Combo Box to strings with a dictionary:

lookup_dictionary[ui.ComboBox.currentText()]
Community
  • 1
  • 1
Jesse Reza Khorasanee
  • 3,140
  • 4
  • 36
  • 53