ich habe mir für einen DataWidgetMapper ein eigenes Delegate geschrieben. Im Grunde genommen macht es das Gleiche wie ein QSqlRelationalDelegate mit dem Unterschied, dass es auch die Möglichkeit gibt keinen Eintrag auszuwählen.
Code: Alles auswählen
void LSoftComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QComboBox* combo = qobject_cast<QComboBox*>(editor);
for (int i = 0; i < combo->model()->rowCount(); i++)
{
if (combo->model()->index(i, 0).data().toInt() == index.data().toInt())
{
combo->setCurrentIndex(i);
return;
}
}
combo->setCurrentIndex(-1);
}
void LSoftComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (!index.isValid())
return;
QComboBox* combo = qobject_cast<QComboBox*>(editor);
if (!combo->currentText().isEmpty())
{
for (int i = 0; i < combo->model()->rowCount(); i++)
{
if (combo->currentText() == combo->model()->index(i, combo->modelColumn()).data().toString())
{
model->setData(index, combo->model()->index(i, 0).data());
return;
}
}
}
model->setData(index, 0);
}Mein Tablemodel hat als EditBehavior OnFieldChange und die ComboBox wird mit setModel(...) gefüllt.
Gruß Michael