In my application, I have a QSqlTableModel filled with a table coming from a MySQL connection. To show its contents, I use a QTableView; and to edit and display it, I subclassed QStyledItemDelegate.
I need to have the ability to edit an entire row of the model. To accomplish this, I use the QModelIndex::sibling method in order to retrieve/set all values in that row, regardless of which (row/column) pair “activated” the delegate.
With that, I can build a QDialog subclass (my delegate editor) and fill it with the current values of the selected row. And then let the user edit the content and after commit the changes or discard them, depending on whether the user clicked “save” or “cancel”.
The problem is: I use QAbstractItemModel::setData to save changes back into model. But under certain conditions (still unknown), this method calls QAbstractItemDelegate::setEditorData, which overwrites the new content that is in QDialog subclass for the old content, currently present in the model.
When this happens, any next call to QAbstractItemModel::setData will write the content contained in the model in itself, rather than the new value selected by the user, because the previous call to QAbstractItemDelegate::setEditorData made the QDialog subclass lose all the information chosen by the user, from the column corresponding to the QAbstractItemModel::setData who originated the problem.
Does anyone know how to prevent this problem from occurring?
Some code:
QWidget *MedicationDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/) const
{
EditMedicationDialog *editor = new EditMedicationDialog(parent);
editor->setAttribute(Qt::WA_DeleteOnClose, true);
editor->setModal(true);
connect(editor, SIGNAL(accepted()), this, SLOT(editorAccepted()));
connect(editor, SIGNAL(rejected()), this, SLOT(editorRejected()));
return editor;
}
void MedicationDelegate::setEditorData(QWidget *uncastedEditor, const QModelIndex& index) const
{
int currentRow = index.row();
QModelIndex drugClassIndex = index.sibling(currentRow, MedicationModel::DrugClass);
QModelIndex drugIndex = index.sibling(currentRow, MedicationModel::Drug);
QModelIndex dosageIndex = index.sibling(currentRow, MedicationModel::Dosage);
QModelIndex amountIndex = index.sibling(currentRow, MedicationModel::Amount);
QModelIndex scheduleIndex = index.sibling(currentRow, MedicationModel::Schedule);
QModelIndex frequencyIndex = index.sibling(currentRow, MedicationModel::Frequency);
QString drugClass = index.model()->data(drugClassIndex, Qt::DisplayRole).toString();
QString drug = index.model()->data(drugIndex, Qt::DisplayRole).toString();
QString dosage = index.model()->data(dosageIndex, Qt::DisplayRole).toString();
QString amount = index.model()->data(amountIndex, Qt::DisplayRole).toString();
QString schedule = index.model()->data(scheduleIndex, Qt::DisplayRole).toString();
QString frequency = index.model()->data(frequencyIndex, Qt::DisplayRole).toString();
EditMedicationDialog *editor = qobject_cast<EditMedicationDialog*>(uncastedEditor);
// these methods sets the current row values in the editor
editor->setDrugClass(drugClass);
editor->setDrug(drug);
editor->setDosage(dosage);
editor->setAmount(amount);
editor->setSchedule(schedule);
editor->setFrequency(frequency);
}
void MedicationDelegate::setModelData(QWidget *uncastedEditor, QAbstractItemModel *model, const QModelIndex& index) const
{
int currentRow = index.row();
QModelIndex drugClassIndex = index.sibling(currentRow, MedicationModel::DrugClass);
QModelIndex drugIndex = index.sibling(currentRow, MedicationModel::Drug);
QModelIndex dosageIndex = index.sibling(currentRow, MedicationModel::Dosage);
QModelIndex amountIndex = index.sibling(currentRow, MedicationModel::Amount);
QModelIndex scheduleIndex = index.sibling(currentRow, MedicationModel::Schedule);
QModelIndex frequencyIndex = index.sibling(currentRow, MedicationModel::Frequency);
EditMedicationDialog *editor = qobject_cast<EditMedicationDialog*>(uncastedEditor);
model->setData(drugClassIndex, editor->drugClass());
model->setData(drugIndex, editor->drug()); // HERE
model->setData(dosageIndex, editor->dosage());
model->setData(amountIndex, editor->amount());
model->setData(scheduleIndex, editor->schedule());
model->setData(frequencyIndex, editor->frequency());
}
The line with “HERE” comment is causing the problem (according to debug).
EDIT (new info): I just realize that QAbstractItemModel::setData is always called just after the column that caused the “edit” is saved. I always gave two clicks in the “drug” column. Therefore, QAbstractItemDelegate::setEditorData is called after that edition.
Thanks.
You could use a boolean to prevent the function body to be executed recursively.
A simpler solution would be
QAbstractItemView::NoEditTriggers,clickedof the view to open/show theQDialoginstead of the delegate,QDataWidgetMapper.The same
QDialogcould then be reused, and you would populate the controls withQDataWidgetMapper::setCurrentIndexbefore showing it withexec()and eitherQDataWidgetMapper::submit()orrevert()after the user validates or cancels the changes.Since the
QDataWidgetMapperand theQTableViewwould share the same model, the table view would be automatically updated.