Adding QComboBox to a QTableView and getting/setting values after creation

Hi there,

My first post here :slight_smile: I have been following the book which is very informative but I wasn’t able to find a few things with MVC, especially QTableView. I would like to know how to add a QComboBox and then setting/getting its values after its created.

I managed to add the combo box with QItemDelegate but now I am not sure once it’s added, how can I access it and change the items in the combo box. I mainly want to display values and once a value is selected, later get this selected value to use it in another function.

UPDATE: I managed to change the value too with adding a function in delegate to update the data but now this data is displayed in all combo boxes of the cell. My combo box shows versions so each row needs to show different versions in the combo box!

Any guidance or example is appreciated!

Thanks,
ak

Adding corrected code:

class ComboDelegate(QItemDelegate):
    """
    A delegate to add QComboBox in every cell of the given column
    """

    def __init__(self, parent):
        super(ComboDelegate, self).__init__(parent)
        self.parent = parent

    def createEditor(self, parent, option, index):
        combobox = QComboBox(parent)
        version_list = []
        for item in index.data():
            if item not in version_list:
                version_list.append(item)

        combobox.addItems(version_list)
        combobox.currentTextChanged.connect(lambda value: self.currentIndexChanged(index, value))
        return combobox

    def setEditorData(self, editor, index):
        value = index.data()
        if value:
            maxval = len(value)
            editor.setCurrentIndex(maxval - 1)
1 Like