[Maya-Python]Working with QCombobox and QStandardItem

18 views
Skip to first unread message

Jakob Kousholt

unread,
Apr 22, 2020, 11:39:09 AM4/22/20
to python_inside_maya
Hey Folks, I am looking to create a list within a QCombobox where the items have a human-readable title but is referencing an underlying unique id.

My ComboBox will list a QStandardItemModel, which contains a QStandardItem per project.

This is currently what I am doing:

for project in self.projects:
item = QtGui.QStandardItem()
item.setData(project['title'], QtCore.Qt.DisplayRole)
item.setData(project['id'], QtCore.Qt.UserRole)
item.setCheckable(False)
self.model.appendRow(item)

I then append the QStandardItemModel to the QCombobox and it displays the title as expected - sweet! However, I am not sure how I will go about retrieving the underlying id. Currently, I use projectList.currentText() Which works for getting the
current display value of the combo box, but how do I reach the underlying current QStandardItem and its id?

Also, if there's anything I have misunderstood regarding how to work with these elements I'd be happy if it got pointed out.
Thanks!

Russell Pearsall

unread,
Apr 22, 2020, 3:12:39 PM4/22/20
to Python Programming for Autodesk Maya
you can get the data for any role from the combobox itself

data = combo.currentData(role=QtCore.Qt.UserRole)

Jakob Kousholt

unread,
Apr 22, 2020, 3:25:54 PM4/22/20
to python_inside_maya
Nice! It works like a charm. Thanks, Russel!


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/7bf94952-87b3-4651-98ed-ef779dee08ea%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages