diff options
author | Jesus Fernandez <jesus.fernandez@qt.io> | 2017-02-24 20:02:48 +0100 |
---|---|---|
committer | Jesus Fernandez <Jesus.Fernandez@qt.io> | 2017-04-24 12:23:50 +0000 |
commit | 87f9a44ef04ac12d0180bb556cab7f3528ba036a (patch) | |
tree | 301ea9f193f2b4011dc9be0c9fa825745eaf6b53 /src/sql/models | |
parent | 8bc36c773faf3bc3d68731d9cfafaf6caf2c0783 (diff) |
Fix QSqlRelationDelegate when using quotation marks
Strip the quotes from the field name in QSqlRelationalDelegate
implementation to able to find the field in the dictionary.
Task-number: QTBUG-59137
Change-Id: I2f1dc9ce3b9c91ca6cc3d3b82e61e1456c3b22c7
Reviewed-by: Samuel Gaist <samuel.gaist@edeltech.ch>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Diffstat (limited to 'src/sql/models')
-rw-r--r-- | src/sql/models/qsqlrelationaldelegate.h | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/src/sql/models/qsqlrelationaldelegate.h b/src/sql/models/qsqlrelationaldelegate.h index 32c994363e..a689e88ba3 100644 --- a/src/sql/models/qsqlrelationaldelegate.h +++ b/src/sql/models/qsqlrelationaldelegate.h @@ -47,6 +47,7 @@ #include <QtWidgets/qitemdelegate.h> #include <QtWidgets/qlistview.h> #include <QtWidgets/qcombobox.h> +#include <QtSql/qsqldriver.h> #include <QtSql/qsqlrelationaltablemodel.h> QT_BEGIN_NAMESPACE @@ -54,6 +55,16 @@ QT_BEGIN_NAMESPACE class QSqlRelationalDelegate: public QItemDelegate { + static int fieldIndex(const QSqlTableModel *const model, + const QSqlDriver *const driver, + const QString &fieldName) + { + const QString stripped = driver->isIdentifierEscaped(fieldName, QSqlDriver::FieldName) + ? driver->stripDelimiters(fieldName, QSqlDriver::FieldName) + : fieldName; + return model->fieldIndex(stripped); + } + public: explicit QSqlRelationalDelegate(QObject *aParent = nullptr) @@ -71,10 +82,12 @@ QWidget *createEditor(QWidget *aParent, QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr; if (!childModel) return QItemDelegate::createEditor(aParent, option, index); + const QSqlDriver *const driver = childModel->database().driver(); QComboBox *combo = new QComboBox(aParent); combo->setModel(childModel); - combo->setModelColumn(childModel->fieldIndex(sqlModel->relation(index.column()).displayColumn())); + combo->setModelColumn(fieldIndex(childModel, driver, + sqlModel->relation(index.column()).displayColumn())); combo->installEventFilter(const_cast<QSqlRelationalDelegate *>(this)); return combo; @@ -92,10 +105,13 @@ void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex QItemDelegate::setModelData(editor, model, index); return; } + const QSqlDriver *const driver = childModel->database().driver(); int currentItem = combo->currentIndex(); - int childColIndex = childModel->fieldIndex(sqlModel->relation(index.column()).displayColumn()); - int childEditIndex = childModel->fieldIndex(sqlModel->relation(index.column()).indexColumn()); + int childColIndex = fieldIndex(childModel, driver, + sqlModel->relation(index.column()).displayColumn()); + int childEditIndex = fieldIndex(childModel, driver, + sqlModel->relation(index.column()).indexColumn()); sqlModel->setData(index, childModel->data(childModel->index(currentItem, childColIndex), Qt::DisplayRole), Qt::DisplayRole); |