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.
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. .
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.
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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With