QAbstractTableModel Class

QAbstractTableModel 类提供了一个抽象模型,可对其进行子类化以创建表格模型。更多

Header: #include <QAbstractTableModel>
CMake.QAbstractTableModel find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
继承: QAbstractItemModel
继承于:

QSqlQueryModel

公共函数

QAbstractTableModel(QObject *parent = nullptr)
virtual ~QAbstractTableModel()

重新实现的公共函数

virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
virtual Qt::ItemFlags flags(const QModelIndex &index) const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override
virtual QModelIndex sibling(int row, int column, const QModelIndex &idx) const override

详细说明

QAbstractTableModel 为以二维数组表示数据的模型提供了一个标准接口。它不会被直接使用,但必须被子类化。

由于该模型提供了一个比QAbstractItemModel 更专门的接口,因此它不适合与树视图一起使用,尽管它可以用来向QListView 提供数据。如果您需要表示一个简单的项列表,并且只需要一个包含单列数据的模型,那么子类化QAbstractListModel 可能更合适。

rowCount() 和columnCount() 函数返回表的维数。要检索与模型中某一项相对应的模型索引,请使用index() 并只提供行和列的编号。

子类化

子类化 QAbstractTableModel 时,必须实现rowCount()、columnCount() 和data()。QAbstractTableModel 提供了index() 和parent() 函数的默认实现。行为良好的模型也会实现headerData()。

可编辑模型需要实现setData() 并实现flags() 以返回包含Qt::ItemIsEditable 的值。

为可调整大小的数据结构提供接口的模型可以提供insertRows(),removeRows(),insertColumns() 和removeColumns() 的实现。在实现这些函数时,重要的是要调用适当的函数,以便所有连接的视图都知道任何变化:

注: 模型子类化参考》(Model Subclassing Reference)中提供了一些子类化模型的一般指南。

线程安全

作为QObject 的子类,QAbstractTableModel 不是thread-safe 。任何与 QAbstractTableModel 模型相关的 API 都只能在模型对象所在的线程中调用。如果 QAbstractTableModel 与视图相连,那就意味着视图所在的 GUI 线程,它将从 GUI 线程调用模型。使用后台线程填充或修改模型内容是可行的,但需要小心,因为后台线程不能直接调用任何与模型相关的 API。相反,您应该对更新进行排队,然后在主线程中应用它们。这可以通过队列连接来实现。

另请参阅 模型类QAbstractItemModelQAbstractListModel

成员函数文档

[explicit] QAbstractTableModel::QAbstractTableModel(QObject *parent = nullptr)

为给定的parent 构建抽象表模型。

[virtual noexcept] QAbstractTableModel::~QAbstractTableModel()

销毁抽象表模型。

[override virtual] bool QAbstractTableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)

重实现:QAbstractItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent).

[override virtual] Qt::ItemFlags QAbstractTableModel::flags(const QModelIndex &index) const

重实现:QAbstractItemModel::flags(const QModelIndex &index) const.

[override virtual] QModelIndex QAbstractTableModel::index(int row, int column, const QModelIndex &parent = QModelIndex()) const

重实现:QAbstractItemModel::index(int row, int column, const QModelIndex &parent) 常量。

返回rowcolumnparent 中数据的索引。

另请参阅 parent() 。

[override virtual] QModelIndex QAbstractTableModel::sibling(int row, int column, const QModelIndex &idx) const

重实现:QAbstractItemModel::sibling(int row, int column, const QModelIndex &index) const.

© 2025 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.