6.0.0-rc4Source

QStringListModel Class

The QStringListModel class provides a model that supplies strings to views. More…

Header#include <QStringListModel>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
InheritsQAbstractListModel

List of all members, including inherited members

Public Functions

QStringListModel(QObject *parent = nullptr)
QStringListModel(const QStringList &strings, QObject *parent = nullptr)
~QStringListModel()
QStringListstringList() const
voidsetStringList(const QStringList &strings)

Reimplemented Public Functions

introwCount(const QModelIndex &parent = QModelIndex()) const
QModelIndexsibling(int row, int column, const QModelIndex &idx) const
QVariantdata(const QModelIndex &index, int role = Qt::DisplayRole) const
boolsetData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole)
boolclearItemData(const QModelIndex &index)
Qt::ItemFlagsflags(const QModelIndex &index) const
boolinsertRows(int row, int count, const QModelIndex &parent = QModelIndex())
boolremoveRows(int row, int count, const QModelIndex &parent = QModelIndex())
boolmoveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
QMap<int, QVariant>itemData(const QModelIndex &index) const
boolsetItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
voidsort(int column, Qt::SortOrder order = Qt::AscendingOrder)
Qt::DropActionssupportedDropActions() const

Detailed Description

A QStringListModel is an editable list model whose items are the strings of a QStringList, available under Qt::DisplayRole and Qt::EditRole.

QStringListModel model({u"apple"_s, u"banana"_s});
model.insertRows(1, 1);
model.setData(model.index(1), u"cherry"_s);
model.stringList(); // apple, cherry, banana

See also QAbstractListModel and QStringList.

Differences from Qt

Only the display and edit roles are stored; data for other roles, such as Qt::CheckStateRole, cannot be set.

Member Function Documentation

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

Constructs an empty model with the given parent.

[explicit] QStringListModel::QStringListModel(const QStringList &strings, QObject *parent = nullptr)

Constructs a model with the given parent that holds strings.

[override] QStringListModel::~QStringListModel()

Destroys the model.

[override] bool QStringListModel::clearItemData(const QModelIndex &index)

Reimplements QAbstractListModel::clearItemData().

Makes the string at index empty.

This function was introduced in 6.0.

[override] QVariant QStringListModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const

Reimplements QAbstractListModel::data().

Returns the string at index for Qt::DisplayRole and Qt::EditRole, and an invalid QVariant for other values of role.

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

Reimplements QAbstractListModel::flags().

Returns the flags of the item at index: items are editable and can be dragged; the root accepts drops.

[override] bool QStringListModel::insertRows(int row, int count, const QModelIndex &parent = QModelIndex())

Reimplements QAbstractListModel::insertRows().

Inserts count empty strings before row. parent must be invalid.

[override] QMap<int, QVariant> QStringListModel::itemData(const QModelIndex &index) const

Reimplements QAbstractListModel::itemData().

Returns the display and edit data of the item at index.

[override] bool QStringListModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)

Reimplements QAbstractListModel::moveRows().

Moves count strings from sourceRow to before destinationChild. Both parents, sourceParent and destinationParent, must be invalid.

This function was introduced in 6.0.

[override] bool QStringListModel::removeRows(int row, int count, const QModelIndex &parent = QModelIndex())

Reimplements QAbstractListModel::removeRows().

Removes count strings from row. parent must be invalid.

[override] int QStringListModel::rowCount(const QModelIndex &parent = QModelIndex()) const

Reimplements QAbstractListModel::rowCount().

Returns the number of strings, or 0 for a valid parent.

[override] bool QStringListModel::setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole)

Reimplements QAbstractListModel::setData().

Sets the string at index to value for Qt::DisplayRole or Qt::EditRole (role) and emits dataChanged(). Returns false for other roles or an invalid index.

[override] bool QStringListModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)

Reimplements QAbstractListModel::setItemData().

Sets the string at index from the Qt::EditRole value in roles, or else the Qt::DisplayRole value.

void QStringListModel::setStringList(const QStringList &strings)

Replaces the strings of the model with strings, resetting the model.

See also stringList().

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

Reimplements QAbstractListModel::sibling().

Returns the index at row and column next to idx.

[override] void QStringListModel::sort(int column, Qt::SortOrder order = Qt::AscendingOrder)

Reimplements QAbstractListModel::sort().

Sorts the strings in order; column is ignored. Persistent indexes follow their strings.

QStringList QStringListModel::stringList() const

Returns the strings of the model.

See also setStringList().

[override] Qt::DropActions QStringListModel::supportedDropActions() const

Reimplements QAbstractListModel::supportedDropActions().

Returns Qt::CopyAction and Qt::MoveAction.