QAbstractListModel Class

QAbstractListModel 类提供了一个抽象模型,可通过子类化该模型来创建一维列表模型。更多

Header: #include <QAbstractListModel>
CMake.QAbstractListModel 类 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 widget 提供字符串列表。在这种情况下,我们只需实现rowCount() 函数来返回列表中的条目数,以及data() 函数来从列表中检索条目。

由于模型是一维结构,因此rowCount() 函数将返回模型中项目的总数。columnCount() 函数的实现是为了与所有类型的视图互操作,但默认情况下会告知视图模型只包含一列。

子类化

子类化 QAbstractListModel 时,必须提供rowCount() 和data() 函数的实现。行为良好的模型还会提供headerData() 实现。

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

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

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

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

注: 模型子类化参考中提供了一些关于模型子类化的一般指南。

另请参阅 模型类模型子类化参考QAbstractItemViewQAbstractTableModel

成员函数文档

[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) 常量。

返回rowcolumnparent 中数据的索引。

另请参阅 parent() 。

[override virtual] QModelIndex QAbstractListModel::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.