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

女生的网名这么多〃 提交于 2019-12-08 19:12:04

问题


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.


回答1:


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.




回答2:


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.




回答3:


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()]


来源:https://stackoverflow.com/questions/35142276/how-can-i-add-item-data-to-qcombobox-from-qt-designer-ui-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!