I already checked a lot of the topics that seemed similar but they didn't help so I am asking this question. If you know of any duplicate question that is already present please direct me to it.
My Situation:
I have a (QCombobox) QItemDelegate for 1 column of a QTableView with a QStandardModel. Now it works fine when I directly edit the value or use the combobox for selection. But i am also trying to provide an option for saving the state of the table so i can reload it when required.
The Problem:
While reloading when I set the item programatically, it doesn't get reflected in the table.
I tried both
1) getting the index & using setData and
2) retrieving the QStandardItem & setting the text.
I also found that the setEditorData is not getting invoked. What am I doing wrong? How do i set the value of such a cell through code?
EDIT: some details
I subclassed the QItemDelegate as DropDown.
dropdown.h
#ifndef DROPDOWN_H
#define DROPDOWN_H
#include <QItemDelegate>
#include <QWidget>
#include <QStringListModel>
#include <QStringList>
class DropDown : public QItemDelegate
{
Q_OBJECT
public:
explicit DropDown(QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setComboData(QStringList list);
QStringList getComboData();
private:
QStringListModel *listmodel;
signals:
public slots:
};
#endif // DROPDOWN_H
dropdown.cpp
#include "dropdown.h"
#include <QComboBox>
DropDown::DropDown(QObject *parent) :
QItemDelegate(parent)
{
listmodel = new QStringListModel(parent);
}
QWidget *DropDown::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QComboBox *editor = new QComboBox(parent);
editor->setEditable(true);
editor->setInsertPolicy(QComboBox::NoInsert);
editor->setModel(listmodel);
return editor;
}
void DropDown::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString value = index.model()->data(index,Qt::EditRole).toString();
QComboBox *original = static_cast<QComboBox *>(editor);
original->setCurrentIndex(original->findText(value));
}
void DropDown::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *original = static_cast<QComboBox *>(editor);
QString value = original->currentText();
model->setData(index, value, Qt::EditRole);
}
void DropDown::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
void DropDown::setComboData(QStringList list)
{
listmodel->setStringList(list);
}
QStringList DropDown::getComboData()
{
return listmodel->stringList();
}
and this is how it is used in the table.
model = new QStandardItemModel(0,11, this);//i update row count later while adding items.
ui->itemTable->setModel(model);
sourceSet = new DropDown(this);
ui->itemTable->setItemDelegateForColumn(3, sourceSet);