QStringListModel Class
QStringListModel 类提供了一个向视图提供字符串的模型。更多内容...
| 头文件: | #include <QStringListModel> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
| 继承自: | QAbstractListModel |
| 被继承者: |
公共函数
| QStringListModel(QObject *parent = nullptr) | |
| QStringListModel(const QStringList &strings, QObject *parent = nullptr) | |
| void | setStringList(const QStringList &strings) |
| QStringList | stringList() const |
重新实现的公共函数
(since 6.0) virtual bool | clearItemData(const QModelIndex &index) override |
| virtual QVariant | data(const QModelIndex &index, int role = Qt::DisplayRole) const override |
| virtual Qt::ItemFlags | flags(const QModelIndex &index) const override |
| virtual bool | insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override |
| virtual QMap<int, QVariant> | itemData(const QModelIndex &index) const override |
| virtual bool | moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override |
| virtual bool | removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override |
| virtual int | rowCount(const QModelIndex &parent = QModelIndex()) const override |
| virtual bool | setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override |
| virtual bool | setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) override |
| virtual QModelIndex | sibling(int row, int column, const QModelIndex &idx) const override |
| virtual void | sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override |
| virtual Qt::DropActions | supportedDropActions() const override |
详细说明
QStringListModel 是一种可编辑模型,适用于需要在视图控件(如QListView 或QComboBox )中显示多个字符串的简单场景。
该模型提供了可编辑模型的所有标准功能,将字符串列表中的数据表示为一个模型,该模型包含一列,行数等于列表中的项目数。
通过index()函数可获取与项目对应的模型索引,通过flags()函数可获取项目标志。项目数据的读取使用data()函数,写入则使用setData()函数。行数(即字符串列表中的项目数)可通过rowCount()函数获取。
该模型可以使用现有的字符串列表构建,也可以通过便利函数setStringList() 在后续设置字符串。还可以通过insertRows() 函数以常规方式插入字符串,并通过removeRows() 函数移除字符串。字符串列表的内容可通过便利函数stringList() 获取。
QStringListModel 的使用示例:
QStringListModel *model = new QStringListModel();
QStringList list;
list << "a" << "b" << "c";
model->setStringList(list);另请参阅 QAbstractListModel 、QAbstractItemModel 、QRangeModel 以及模型类。
成员函数文档
[explicit] QStringListModel::QStringListModel(QObject *parent = nullptr)
根据给定的parent 构建一个字符串列表模型。
[explicit] QStringListModel::QStringListModel(const QStringList &strings, QObject *parent = nullptr)
使用给定的parent ,构建一个包含指定strings 的字符串列表模型。
[override virtual, since 6.0] bool QStringListModel::clearItemData(const QModelIndex &index)
重写了:QAbstractItemModel::clearItemData(const QModelIndex &index)。
该函数在 Qt 6.0 中引入。
[override virtual] QVariant QStringListModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const
重写:QAbstractItemModel::data(const QModelIndex &index, int role) const。
返回指定role 对应的数据,该索引来自具有给定index 的项。
如果视图请求的索引无效,则返回一个无效的变体。
另请参阅 setData()。
[override virtual] Qt::ItemFlags QStringListModel::flags(const QModelIndex &index) const
重写:QAbstractListModel::flags(const QModelIndex &index) const。
返回具有给定index 的项目的标志。
有效的项目包括:enabled、selectable、editable、drag enabled 和 drop enabled。
另请参阅 QAbstractItemModel::flags()。
[override virtual] bool QStringListModel::insertRows(int row, int count, const QModelIndex &parent = QModelIndex())
重写了:QAbstractItemModel::insertRows(int row, int count, const QModelIndex &parent)。
将count 行插入模型中,从给定的row 开始。
行对应的parent 索引是可选的,仅用于与QAbstractItemModel 保持一致。默认情况下,指定为空索引,表示这些行将插入到模型的顶层。
若插入成功,则返回 `true `。
另请参阅 QAbstractItemModel::insertRows()。
[override virtual] QMap<int, QVariant> QStringListModel::itemData(const QModelIndex &index) const
重写了:QAbstractItemModel::itemData(const QModelIndex &index) const。
另请参阅 setItemData()。
[override virtual] bool QStringListModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
重写了以下函数:QAbstractItemModel::moveRows (const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)。
[override virtual] bool QStringListModel::removeRows(int row, int count, const QModelIndex &parent = QModelIndex())
重写:QAbstractItemModel::removeRows (int row, int count, const QModelIndex &parent)。
从模型中移除count 行,从给定的row 开始。
行索引 `parent ` 为可选参数,仅用于与 `QAbstractItemModel` 保持一致。默认情况下,指定为空索引,表示在模型的顶层删除行。
如果行删除成功,则返回 `true `。
另请参阅 QAbstractItemModel::removeRows()。
[override virtual] int QStringListModel::rowCount(const QModelIndex &parent = QModelIndex()) const
重写:QAbstractItemModel::rowCount(const QModelIndex &parent) const。
返回模型中的行数。该值对应于模型内部字符串列表中的项目数量。
可选参数 `parent ` 在大多数模型中用于指定待统计行项的父行。由于若指定了有效的父行,该参数将返回一个列表,因此结果始终为 0。
另请参阅 insertRows()、removeRows() 和QAbstractItemModel::rowCount()。
[override virtual] bool QStringListModel::setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole)
重写:QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value, int role)。
将模型中具有给定index 的项中,指定role 的数据设置为提供的value 。
如果项目发生更改,则会发出dataChanged()信号。在发出dataChanged()信号后,返回true 。
另请参阅 Qt::ItemDataRole 和data()。
[override virtual] bool QStringListModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
重写了:QAbstractItemModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)。
如果 `roles ` 同时包含 `Qt::DisplayRole ` 和 `Qt::EditRole`,则后者优先
另请参阅 itemData()。
void QStringListModel::setStringList(const QStringList &strings)
将模型的内部字符串列表设置为strings 。模型将通知所有关联的视图,其底层数据已发生变化。
另请参阅 stringList() 和dataChanged()。
[override virtual] QModelIndex QStringListModel::sibling(int row, int column, const QModelIndex &idx) const
重写了:QAbstractListModel::sibling(int row, int column, const QModelIndex &idx) const。
[override virtual] void QStringListModel::sort(int column, Qt::SortOrder order = Qt::AscendingOrder)
重写了:QAbstractItemModel::sort(int column, Qt::SortOrder order)。
QStringList QStringListModel::stringList() const
返回模型用于存储数据的字符串列表。
另请参阅 setStringList()。
[override virtual] Qt::DropActions QStringListModel::supportedDropActions() const
重新实现了:QAbstractItemModel::supportedDropActions() 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.