Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qsqlrelationaldelegate.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSQLRELATIONALDELEGATE_H
5#define QSQLRELATIONALDELEGATE_H
6
7#include <QtSql/qtsqlglobal.h>
8
10
11#ifdef QT_WIDGETS_LIB
12
13#include <QtWidgets/qstyleditemdelegate.h>
14#if QT_CONFIG(listview)
15#include <QtWidgets/qlistview.h>
16#endif
17#if QT_CONFIG(combobox)
18#include <QtWidgets/qcombobox.h>
19#endif
20#include <QtSql/qsqldriver.h>
21#include <QtSql/qsqlrelationaltablemodel.h>
22#include <QtCore/qmetaobject.h>
24
26{
27 static int fieldIndex(const QSqlTableModel *const model,
28 const QSqlDriver *const driver,
29 const QString &fieldName)
30 {
32 ? driver->stripDelimiters(fieldName, QSqlDriver::FieldName)
33 : fieldName;
34 return model->fieldIndex(stripped);
35 }
36
37public:
38
39 explicit QSqlRelationalDelegate(QObject *aParent = nullptr)
40 : QStyledItemDelegate(aParent)
41 {}
42
44 {}
45
46 QWidget *createEditor(QWidget *aParent,
47 const QStyleOptionViewItem &option,
48 const QModelIndex &index) const override
49 {
50 const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel *>(index.model());
51 QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr;
52 if (!childModel)
54 const QSqlDriver *const driver = childModel->database().driver();
55
56 QComboBox *combo = new QComboBox(aParent);
57 combo->setModel(childModel);
58 combo->setModelColumn(fieldIndex(childModel, driver,
59 sqlModel->relation(index.column()).displayColumn()));
60 combo->installEventFilter(const_cast<QSqlRelationalDelegate *>(this));
61
62 return combo;
63 }
64
65 void setEditorData(QWidget *editor, const QModelIndex &index) const override
66 {
67 if (!index.isValid())
68 return;
69
70 if (qobject_cast<QComboBox *>(editor)) {
71 // Taken from QItemDelegate::setEditorData() as we need
72 // to present the DisplayRole and not the EditRole which
73 // is the id reference to the related model
75 const QByteArray n = editor->metaObject()->userProperty().name();
76 if (!n.isEmpty()) {
77 if (!v.isValid())
78 v = QVariant(editor->property(n.data()).metaType());
79 editor->setProperty(n.data(), v);
80 return;
81 }
82 }
84 }
85
86void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
87{
88 if (!index.isValid())
89 return;
90
91 QSqlRelationalTableModel *sqlModel = qobject_cast<QSqlRelationalTableModel *>(model);
92 QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr;
93 QComboBox *combo = qobject_cast<QComboBox *>(editor);
94 if (!sqlModel || !childModel || !combo) {
96 return;
97 }
98 const QSqlDriver *const driver = childModel->database().driver();
99
100 int currentItem = combo->currentIndex();
101 int childColIndex = fieldIndex(childModel, driver,
102 sqlModel->relation(index.column()).displayColumn());
103 int childEditIndex = fieldIndex(childModel, driver,
104 sqlModel->relation(index.column()).indexColumn());
105 sqlModel->setData(index,
106 childModel->data(childModel->index(currentItem, childColIndex), Qt::DisplayRole),
108 sqlModel->setData(index,
109 childModel->data(childModel->index(currentItem, childEditIndex), Qt::EditRole),
111}
112
113};
114
116
117#endif // QT_WIDGETS_LIB
118
119#endif // QSQLRELATIONALDELEGATE_H
\inmodule QtCore
Definition qbytearray.h:57
The QComboBox widget combines a button with a dropdown list.
Definition qcombobox.h:24
void setModelColumn(int visibleColumn)
virtual void setModel(QAbstractItemModel *model)
Sets the model to be model.
int currentIndex
the index of the current item in the combobox.
Definition qcombobox.h:31
\inmodule QtCore
\inmodule QtCore
Definition qobject.h:103
void installEventFilter(QObject *filterObj)
Installs an event filter filterObj on this object.
Definition qobject.cpp:2339
The QSqlDriver class is an abstract base class for accessing specific SQL databases.
Definition qsqldriver.h:26
virtual QString stripDelimiters(const QString &identifier, IdentifierType type) const
Returns the identifier with the leading and trailing delimiters removed, identifier can either be a t...
virtual bool isIdentifierEscaped(const QString &identifier, IdentifierType type) const
Returns whether identifier is escaped according to the database rules.
\inmodule QtSql
The QSqlRelationalTableModel class provides an editable data model for a single database table,...
The QSqlTableModel class provides an editable data model for a single database table.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QStyledItemDelegate class provides display and editing facilities for data items from a model.
void setEditorData(QWidget *editor, const QModelIndex &index) const override
Sets the data to be displayed and edited by the editor from the data model item specified by the mode...
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
Gets data from the editor widget and stores it in the specified model at the item index.
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Returns the widget used to edit the item specified by index for editing.
\inmodule QtCore
Definition qvariant.h:65
void * data()
Returns a pointer to the contained object as a generic void* that can be written to.
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
Combined button and popup list for selecting options.
@ EditRole
@ DisplayRole
static glyph_t stripped(glyph_t glyph)
GLsizei const GLfloat * v
[13]
GLuint index
[2]
GLfloat n
GLuint GLenum option
#define QT_REQUIRE_CONFIG(feature)
QSqlQueryModel * model
[16]