Search code examples
pythonpyqt4pysidepyqt5pyside2

How to set data to QComboBox using QAbstractTableModel (Model/View)?


I wish to be able to set the itemData of a combobox when populated using a QAbstractTableModel. However, I can only return one string from the model's data method.

Usually, when not using a model, this can be performed like so:

# Set text and data
combobox.addItem('Some text', 'some item data')

# Retrieve data from selected
item_data = combobox.itemData(combobox.currentIndex())

How can this be done, but using QAbstractTableModel?


I have a combobox, which I set a model to:

model = ProjectTableModel(projects)
combobox.setModel(model)

My model:

class ProjectTableModel(QtCore.QAbstractTableModel):

    def __init__(self, projects=[], parent=None):
        QtCore.QAbstractTableModel.__init__(self, parent)
        self._projects = projects

    def rowCount(self, parent):
        return len(self._projects)

    def columnCount(self, parent):
        return 2

    def data(self, index, role):
        row = index.row()
        column = index.column()

        if role == QtCore.Qt.DisplayRole and column == 0:
            project = self._projects[row]
            name = project.name()
            id = project.id()  # <----- how to add this as itemData?
            return name

Solution

  • A QComboBox always uses a model to store its data. If you don't set a model yourself, it will create its own QStandardItemModel. Methods such as addItem and itemData simply store and retrieve values using whatever underlying model has been set. By default, the combo-box uses the Qt.UserRole to store item-data in the model. So your model just needs to do something like this:

    def data(self, index, role):
        row = index.row()
        column = index.column()
    
        if role == QtCore.Qt.DisplayRole and column == 0:
            project = self._projects[row]
            name = project.name()
            return name
        elif role == QtCore.Qt.UserRole and column == 0:
            project = self._projects[row]
            id = project.id()
            return id
    
    def setData(self, index, value, role):
        row = index.row()
        column = index.column()
    
        if role == QtCore.Qt.UserData and column == 0:
            project = self._projects[row]
            project.setId(value) # or whatever