QSqlRelationalDelegate Class
QSqlRelationalDelegate 类提供了一个委托,用于显示和编辑来自QSqlRelationalTableModel 的数据。更多内容...
| 头文件: | #include <QSqlRelationalDelegate> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Sql) target_link_libraries(mytarget PRIVATE Qt6::Sql) |
| qmake: | QT += sql |
| 继承自: | QStyledItemDelegate |
公共函数
| QSqlRelationalDelegate(QObject *parent = nullptr) | |
| virtual | ~QSqlRelationalDelegate() |
重新实现的公共函数
| virtual QWidget * | createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override |
| virtual void | setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override |
详细说明
与默认委托不同,QSqlRelationalDelegate 为作为其他表外键的字段提供了一个下拉列表。要使用该类,只需在视图上使用 QSqlRelationalDelegate 的实例调用QAbstractItemView::setItemDelegate() 即可:
std::unique_ptr<QTableView> view{new QTableView};
view->setModel(model);
view->setItemDelegate(new QSqlRelationalDelegate(view.get()));“关系表模型”示例(如下所示)演示了如何将 QSqlRelationalDelegate 与QSqlRelationalTableModel 结合使用,为表提供外键支持。

另请参阅 QSqlRelationalTableModel 和模型/视图编程。
成员函数文档
[explicit] QSqlRelationalDelegate::QSqlRelationalDelegate(QObject *parent = nullptr)
使用给定的parent 构建一个QSqlRelationalDelegate对象。
[virtual noexcept] QSqlRelationalDelegate::~QSqlRelationalDelegate()
销毁QSqlRelationalDelegate 对象,并释放所有已分配的资源。
[override virtual] QWidget *QSqlRelationalDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
重写了:QStyledItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const。
[override virtual] void QSqlRelationalDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
重写了:QStyledItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.