本页内容

QAbstractListModel Class

QAbstractListModel 类提供了一个抽象模型,可以通过继承该类来创建一维列表模型。更多内容...

头文件: #include <QAbstractListModel>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
继承自: QAbstractItemModel
被继承者:

QPdfLinkModel、QPdfSearchModel 、QStringListModel 、QVirtualKeyboardSelectionListModel 以及QWebEngineHistoryModel

公共函数

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

重新实现的公共函数

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 = 0, const QModelIndex &parent = QModelIndex()) const override
virtual QModelIndex sibling(int row, int column, const QModelIndex &idx) const override

详细说明

QAbstractListModel 为将数据表示为简单非层次化项目序列的模型提供了一个标准接口。它不能直接使用,必须通过子类来继承。

由于该模型提供的接口比QAbstractItemModel 更专业,因此不适用于树形视图;若要为此目的提供模型,则需要继承QAbstractItemModel 。若需要使用多个列表模型来管理数据,则继承QAbstractTableModel 可能更为合适。

可以通过继承该类并实现最少数量的必备函数来创建简单的模型。 例如,我们可以实现一个基于QStringList 的简单只读模型,为QListView 控件提供一个字符串列表。在这种情况下,我们只需实现rowCount() 函数来返回列表中的项目数量,以及data() 函数来从列表中检索项目。

由于该模型表示一维结构,因此rowCount() 函数返回模型中项目的总数。columnCount() 函数的实现是为了与各类视图兼容,但默认情况下会告知视图该模型仅包含一列。

子类化

在继承 QAbstractListModel 时,必须提供rowCount() 和data() 函数的实现。行为规范的模型还会提供headerData() 的实现。

如果您的模型在 QML 中使用,并且需要roleNames() 函数提供的默认角色之外的其他角色,则必须重写该函数。

对于可编辑的列表模型,您还必须提供setData() 的实现,并实现flags() 函数,使其返回一个包含Qt::ItemIsEditable 的值。

请注意,QAbstractListModel 提供了columnCount() 的默认实现,该实现会告知视图此模型中仅有一列项目。

为可调整大小的列表式数据结构提供接口的模型可以实现insertRows() 和removeRows() 方法。在实现这些方法时,必须调用相应的函数,以确保所有关联的视图都能感知到任何变化:

注意: 《模型子类化参考》中提供了有关模型子类化的一些 通用指南。

另请参阅 “模型类”、“模型子类化参考”、“QAbstractItemView ”、“QAbstractTableModel ”和“QRangeModel ”。

成员函数文档

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

根据给定的parent 构建一个抽象列表模型。

[virtual noexcept] QAbstractListModel::~QAbstractListModel()

销毁抽象列表模型。

[override virtual] bool QAbstractListModel::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 QAbstractListModel::flags(const QModelIndex &index) const

重写了:QAbstractItemModel::flags(const QModelIndex &index) const。

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

重写了:QAbstractItemModel::index(int row, int column, const QModelIndex &parent) const。

返回在row 和column 中,使用parent 对数据进行索引的结果。

另请参阅 parent()。

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

重写了:QAbstractItemModel::sibling(int row, int column, 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.