Search code examples
qtqtableviewqcombobox

How to refresh contents of QCombobox in QTableView


I have QTableView which has QComboBox in one of the columns. The combobox is displaying data from a vector which get updates when I click a button. When I start the application the combobox displays all the items in vector. Now I press the button (which adds more items to the vector) but the combobox doesn't reflect new data in vector. It still shows old data. I am also emitting dataChanged() once the vector is updated but I don't see any change. data() function does get call in the model which does return all the elements of the vector, but setEditorData doesn't get call in delegate.

Am I missing something.

Thanks,

Dev


Solution

  • Then you need to do something like this function:

    void updateComboBox(QComboBox *comboToUpdate, const QStringList & list )
    {
        QString curentText = comboToUpdate->currntText();
        comboToUpdate->clear();
        comboToUpdate->insertItems(list);
        comboToUpdate->setCurrentIndex(comboToUpdate->findText(currentText));
    }
    

    Lines

    QString currentText = comboToUpdate->currentText();
    ...
    comboToUpdate->setCurrentIndex(comboToUpdate->findText(currentText));
    

    are optional and used to don't change currentItem after selection.