From b92db8a4adf985fe842ec0693e17c81d9e816b93 Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Tue, 15 May 2018 15:05:29 +0200 Subject: Show the display role inside the editor for the relation in a QComboBox When a QComboBox is used as the editor for a relation inside a view then it could end up showing the contents of the EditRole. This would be the field which is used to represent the entry as opposed to the DisplayRole which is what the user would expect to see is. Therefore, setEditorData() is overridden to ensure that it is showing the right data to the user. When the model gets updated, it will take the corresponding EditRole value as before to ensure it is updated correctly. Task-number: QTBUG-59632 Change-Id: Ibbccc3e9477de1cdefb654051b97dd111df36382 Reviewed-by: Jesus Fernandez --- src/sql/models/qsqlrelationaldelegate.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/sql/models/qsqlrelationaldelegate.h b/src/sql/models/qsqlrelationaldelegate.h index 0af87f64ae..e8ae5a229d 100644 --- a/src/sql/models/qsqlrelationaldelegate.h +++ b/src/sql/models/qsqlrelationaldelegate.h @@ -55,7 +55,7 @@ QT_REQUIRE_CONFIG(sqlmodel); #endif #include #include - +#include QT_BEGIN_NAMESPACE @@ -99,6 +99,27 @@ QWidget *createEditor(QWidget *aParent, return combo; } + void setEditorData(QWidget *editor, const QModelIndex &index) const override + { + if (!index.isValid()) + return; + + if (qobject_cast(editor)) { + // Taken from QItemDelegate::setEditorData() as we need + // to present the DisplayRole and not the EditRole which + // is the id reference to the related model + QVariant v = index.data(Qt::DisplayRole); + QByteArray n = editor->metaObject()->userProperty().name(); + if (!n.isEmpty()) { + if (!v.isValid()) + v = QVariant(editor->property(n).userType(), nullptr); + editor->setProperty(n, v); + return; + } + } + QItemDelegate::setEditorData(editor, index); + } + void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override { if (!index.isValid()) -- cgit v1.2.3