PDA

View Full Version : Problem with storing data of QTableView to QSatndardItemModel which has delegate



IndikaU
4th October 2011, 07:52
Hi,

I'm using a QTableView to present data of QStandardItemModel. I'm using a combobox delegate for the second column. I want to store data in my custom data structure too. However when I catch the itemChanged(QStandardItem*) signal I don't get the current value of the cell (data = "", which is not in the column 1). But the data entered in these columns remains even the selected cell changed. So they are stored in the model. When I do a run time debug I found that setModelData only emitted if the combobox changed. In normal cell editing this signal is not emitting.

Here's the code fragment to clear up.



//Model
p_TableFields = new QStandardItemModel(this);
p_TableFields->insertColumns(0, 4);
p_TableFields->insertRows(0, 3);

//Table View
tblFields->setModel(p_TableFields);

//ComboBox delegation for column 1
p_delegate = new DelegateComboBox(tblFields);
tblFields->setItemDelegateForColumn(1, p_delegate);

//Signal for data change
connect(p_TableFields, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(CellEdited(QStandardItem*)));

//Cell Edited function
QString value = item->data().toString(); //this is ""(empty)


//Custom delegation - Only fired for column 1
setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index ) const
{
if ( index.column() == 1 )
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QString str = comboBox->currentText();

if ( str.compare("<select>") )
model->setData(index, str);

return;
}

QItemDelegate::setModelData(editor, model, index);
}