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>
This commit is contained in:
parent
8bc36c773f
commit
87f9a44ef0
@ -47,6 +47,7 @@
|
|||||||
#include <QtWidgets/qitemdelegate.h>
|
#include <QtWidgets/qitemdelegate.h>
|
||||||
#include <QtWidgets/qlistview.h>
|
#include <QtWidgets/qlistview.h>
|
||||||
#include <QtWidgets/qcombobox.h>
|
#include <QtWidgets/qcombobox.h>
|
||||||
|
#include <QtSql/qsqldriver.h>
|
||||||
#include <QtSql/qsqlrelationaltablemodel.h>
|
#include <QtSql/qsqlrelationaltablemodel.h>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
@ -54,6 +55,16 @@ QT_BEGIN_NAMESPACE
|
|||||||
|
|
||||||
class QSqlRelationalDelegate: public QItemDelegate
|
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:
|
public:
|
||||||
|
|
||||||
explicit QSqlRelationalDelegate(QObject *aParent = nullptr)
|
explicit QSqlRelationalDelegate(QObject *aParent = nullptr)
|
||||||
@ -71,10 +82,12 @@ QWidget *createEditor(QWidget *aParent,
|
|||||||
QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr;
|
QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr;
|
||||||
if (!childModel)
|
if (!childModel)
|
||||||
return QItemDelegate::createEditor(aParent, option, index);
|
return QItemDelegate::createEditor(aParent, option, index);
|
||||||
|
const QSqlDriver *const driver = childModel->database().driver();
|
||||||
|
|
||||||
QComboBox *combo = new QComboBox(aParent);
|
QComboBox *combo = new QComboBox(aParent);
|
||||||
combo->setModel(childModel);
|
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));
|
combo->installEventFilter(const_cast<QSqlRelationalDelegate *>(this));
|
||||||
|
|
||||||
return combo;
|
return combo;
|
||||||
@ -92,10 +105,13 @@ void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex
|
|||||||
QItemDelegate::setModelData(editor, model, index);
|
QItemDelegate::setModelData(editor, model, index);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const QSqlDriver *const driver = childModel->database().driver();
|
||||||
|
|
||||||
int currentItem = combo->currentIndex();
|
int currentItem = combo->currentIndex();
|
||||||
int childColIndex = childModel->fieldIndex(sqlModel->relation(index.column()).displayColumn());
|
int childColIndex = fieldIndex(childModel, driver,
|
||||||
int childEditIndex = childModel->fieldIndex(sqlModel->relation(index.column()).indexColumn());
|
sqlModel->relation(index.column()).displayColumn());
|
||||||
|
int childEditIndex = fieldIndex(childModel, driver,
|
||||||
|
sqlModel->relation(index.column()).indexColumn());
|
||||||
sqlModel->setData(index,
|
sqlModel->setData(index,
|
||||||
childModel->data(childModel->index(currentItem, childColIndex), Qt::DisplayRole),
|
childModel->data(childModel->index(currentItem, childColIndex), Qt::DisplayRole),
|
||||||
Qt::DisplayRole);
|
Qt::DisplayRole);
|
||||||
|
Loading…
Reference in New Issue
Block a user