QStringListModel Class

QStringListModel 类提供了一个向视图提供字符串的模型。更多

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

QHelpIndexModel

公共函数

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 是一个可编辑模型,可用于需要在视图部件(如QListViewQComboBox )中显示若干字符串的简单情况。

该模型提供了可编辑模型的所有标准功能,将字符串列表中的数据表示为一个模型,该模型有一列,行数等于列表中的项数。

与项相对应的模型索引通过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, 和模型类

成员函数文档

[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.

从具有给定index 的项目中返回指定role 的数据。

如果视图请求的索引无效,则返回无效的变量。

另请参阅 setData() 。

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

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

返回具有给定index 的项目的标志。

有效项目包括启用、可选择、可编辑、可拖动和可下放。

另请参阅 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::ItemDataRoledata()。

[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::DisplayRoleQt::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.

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