The QSqlRelationalTableModel class provides an editable data model for a single database table, with foreign key support. 更多...
头: | #include <QSqlRelationalTableModel> |
CMake: |
find_package(Qt6 COMPONENTS Sql REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Sql) |
qmake: | QT += sql |
继承: | QSqlTableModel |
enum | JoinMode { InnerJoin, LeftJoin } |
QSqlRelationalTableModel (QObject * parent = nullptr, const QSqlDatabase & db = QSqlDatabase()) | |
virtual | ~QSqlRelationalTableModel () |
QSqlRelation | relation (int column ) const |
virtual QSqlTableModel * | relationModel (int column ) const |
void | setJoinMode (QSqlRelationalTableModel::JoinMode joinMode ) |
virtual void | setRelation (int column , const QSqlRelation & relation ) |
virtual void | clear () override |
virtual QVariant | data (const QModelIndex & index , int role = Qt::DisplayRole) const override |
virtual bool | removeColumns (int column , int count , const QModelIndex & parent = QModelIndex()) override |
virtual bool | select () override |
virtual bool | setData (const QModelIndex & index , const QVariant & value , int role = Qt::EditRole) override |
virtual void | setTable (const QString & table ) override |
virtual void | revertRow (int row ) override |
virtual bool | insertRowIntoTable (const QSqlRecord & values ) override |
virtual QString | orderByClause () const override |
virtual QString | selectStatement () const override |
virtual bool | updateRowInTable (int row , const QSqlRecord & values ) override |
QSqlRelationalTableModel acts like QSqlTableModel , but allows columns to be set as foreign keys into other database tables.
![]() |
![]() |
The screenshot on the left shows a plain
QSqlTableModel
在
QTableView
. Foreign keys (
city
and
country
) aren't resolved to human-readable values. The screenshot on the right shows a QSqlRelationalTableModel, with foreign keys resolved into human-readable text strings.
The following code snippet shows how the QSqlRelationalTableModel was set up:
model->setTable("employee"); model->setRelation(2, QSqlRelation("city", "id", "name")); model->setRelation(3, QSqlRelation("country", "id", "name"));
setRelation
() function calls establish a relationship between two tables. The first call specifies that column 2 in table
employee
is a foreign key that maps with field
id
of table
city
, and that the view should present the
city
's
名称
field to the user. The second call does something similar with column 3.
If you use a read-write QSqlRelationalTableModel, you probably want to use QSqlRelationalDelegate on the view. Unlike the default delegate, QSqlRelationalDelegate provides a combobox for fields that are foreign keys into other tables. To use the class, simply call QAbstractItemView::setItemDelegate () on the view with an instance of QSqlRelationalDelegate :
std::unique_ptr<QTableView> view{new QTableView}; view->setModel(model); view->setItemDelegate(new QSqlRelationalDelegate(view.get()));
relationaltablemodel example illustrates how to use QSqlRelationalTableModel in conjunction with QSqlRelationalDelegate to provide tables with foreign key support.
注意事项:
另请参阅 QSqlRelation , QSqlRelationalDelegate ,和 关系表模型范例 .
常量 | 值 | 描述 |
---|---|---|
QSqlRelationalTableModel::InnerJoin
|
0
|
- Inner join mode, return rows when there is at least one match in both tables. |
QSqlRelationalTableModel::LeftJoin
|
1
|
- Left join mode, returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2). |
另请参阅 QSqlRelationalTableModel::setJoinMode ().
Creates an empty QSqlRelationalTableModel and sets the parent to parent and the database connection to db 。若 db is not valid, the default database connection will be used.
[override virtual slot]
void
QSqlRelationalTableModel::
revertRow
(
int
row
)
重实现: QSqlTableModel::revertRow (int row).
[virtual]
QSqlRelationalTableModel::
~QSqlRelationalTableModel
()
销毁对象并释放任何分配资源。
[override virtual]
void
QSqlRelationalTableModel::
clear
()
重实现: QSqlTableModel::clear ().
[override virtual]
QVariant
QSqlRelationalTableModel::
data
(const
QModelIndex
&
index
,
int
role
= Qt::DisplayRole) const
重实现: QSqlTableModel::data (const QModelIndex &index, int role) const.
另请参阅 setData ().
[override virtual protected]
bool
QSqlRelationalTableModel::
insertRowIntoTable
(const
QSqlRecord
&
values
)
重实现: QSqlTableModel::insertRowIntoTable (const QSqlRecord &values).
[override virtual protected]
QString
QSqlRelationalTableModel::
orderByClause
() const
重实现: QSqlTableModel::orderByClause () const.
Returns the relation for the column column , or an invalid relation if no relation is set.
另请参阅 setRelation () 和 QSqlRelation::isValid ().
[virtual]
QSqlTableModel
*QSqlRelationalTableModel::
relationModel
(
int
column
) const
返回
QSqlTableModel
object for accessing the table for which
column
is a foreign key, or
nullptr
if there is no relation for the given
column
.
The returned object is owned by the QSqlRelationalTableModel .
另请参阅 setRelation () 和 relation ().
[override virtual]
bool
QSqlRelationalTableModel::
removeColumns
(
int
column
,
int
count
, const
QModelIndex
&
parent
= QModelIndex())
重实现: QSqlTableModel::removeColumns (int column, int count, const QModelIndex &parent).
[override virtual]
bool
QSqlRelationalTableModel::
select
()
重实现: QSqlTableModel::select ().
[override virtual protected]
QString
QSqlRelationalTableModel::
selectStatement
() const
重实现: QSqlTableModel::selectStatement () const.
[override virtual]
bool
QSqlRelationalTableModel::
setData
(const
QModelIndex
&
index
, const
QVariant
&
value
,
int
role
= Qt::EditRole)
重实现: QSqlTableModel::setData (const QModelIndex &index, const QVariant &value, int role).
Sets the data for the role in the item with the specified index 到 value given. Depending on the edit strategy, the value might be applied to the database at once, or it may be cached in the model.
返回
true
if the value could be set, or false on error (for example, if
index
is out of bounds).
For relational columns,
value
must be the index, not the display value. The index must also exist in the referenced table, otherwise the function returns
false
.
另请参阅 editStrategy (), data (), submit (),和 revertRow ().
Sets the SQL joinMode to show or hide rows with NULL foreign keys. In InnerJoin mode (the default) these rows will not be shown: use the LeftJoin mode if you want to show them.
另请参阅 QSqlRelationalTableModel::JoinMode .
[virtual]
void
QSqlRelationalTableModel::
setRelation
(
int
column
, const
QSqlRelation
&
relation
)
Lets the specified column be a foreign index specified by relation .
范例:
model->setTable("employee"); model->setRelation(2, QSqlRelation("city", "id", "name"));
The setRelation() call specifies that column 2 in table
employee
is a foreign key that maps with field
id
of table
city
, and that the view should present the
city
's
名称
field to the user.
Note: The table's primary key may not contain a relation to another table.
另请参阅 relation ().
[override virtual]
void
QSqlRelationalTableModel::
setTable
(const
QString
&
table
)
重实现: QSqlTableModel::setTable (const QString &tableName).
[override virtual protected]
bool
QSqlRelationalTableModel::
updateRowInTable
(
int
row
, const
QSqlRecord
&
values
)
重实现: QSqlTableModel::updateRowInTable (int row, const QSqlRecord &values).