How to add item data to QComboBox from Qt Designer / .ui file

I use Qt Designer (well, actually, Qt Creator, but specifically the part obtained from Qt Designer), and I added several QComboBox elements to the dialog with a constant list of elements. I need to map items in a combo box to strings (which are different from the displayed strings). The best idea I came up with is to use the QComboBox :: itemData function to get the desired row from the selected item, but I am unable to add the related rows to the items. I looked at the designer and have not yet seen a way to add user data. Is there anything that I miss? I also want to edit the XML of the .ui file directly to add a property if necessary, but I cannot figure out what the name of the property will be. Is there one that I can use here? I am currently adding data to code,but this does not seem to be the right solution for me.

+6
source share
3 answers

Ok, so I really looked through the uic source code and found a place that QComboBox handles. Since the current version of Qt (as 5.5.1), there is no support for setting the attribute of these elements through .ui files. I can make this function request, but so far it is not supported.

+6
source

Change: because this answer seems to get more votes than it should. I will leave it here because many people seem to find this answer when they google for their problem. The answer below is much better for an OP response.

In QT 5.6.2 Designer, you can double-click on the combo box to add items.

+16

:

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

Map combo box to dictionary strings:

lookup_dictionary[ui.ComboBox.currentText()]
0
source

Source: https://habr.com/ru/post/1626934/


All Articles