Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 492
LRFLEW Avatar asked Feb 01 '16 23:02

LRFLEW


People also ask

How do I edit a Qt UI file?

In Qt Creator, right-click on the file in the list and select "edit with... plain text editor". You can copy/paste then. Or use your favorite text editor and do it there. .

How do you make a .UI file?

To create a . ui file go to File -> New File or Project... In the window that appears select Qt under Files and Classes on the left, then select Qt Designer Form on the right. You'll notice the icon has "ui" on it, showing the type of file you're creating.

What is .UI file in Qt?

ui file is used to create a ui_calculatorform. h file that can be used by any file listed in the SOURCES declaration. Note: You can use Qt Creator to create the Calculator Form project. It automatically generates the main.


2 Answers

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)
like image 51
Matheus Torquato Avatar answered Oct 25 '22 09:10

Matheus Torquato


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.

like image 30
LRFLEW Avatar answered Oct 25 '22 10:10

LRFLEW