QStringListModel Class
The QStringListModel class provides a model that supplies strings to views. More…
| Header | #include <QStringListModel> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Inherits | QAbstractListModel |
List of all members, including inherited members
Public Functions
| QStringListModel(QObject *parent = nullptr) | |
| QStringListModel(const QStringList &strings, QObject *parent = nullptr) | |
| ~QStringListModel() | |
| QStringList | stringList() const |
| void | setStringList(const QStringList &strings) |
Reimplemented Public Functions
| int | rowCount(const QModelIndex &parent = QModelIndex()) const |
| QModelIndex | sibling(int row, int column, const QModelIndex &idx) const |
| QVariant | data(const QModelIndex &index, int role = Qt::DisplayRole) const |
| bool | setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) |
| bool | clearItemData(const QModelIndex &index) |
| Qt::ItemFlags | flags(const QModelIndex &index) const |
| bool | insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) |
| bool | removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) |
| bool | moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) |
| QMap<int, QVariant> | itemData(const QModelIndex &index) const |
| bool | setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) |
| void | sort(int column, Qt::SortOrder order = Qt::AscendingOrder) |
| Qt::DropActions | supportedDropActions() 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
QStringListModel::QStringListModel(QObject *parent = nullptr)
Constructs an empty model with the given parent.
QStringListModel::QStringListModel(const QStringList &strings, QObject *parent = nullptr)
Constructs a model with the given parent that holds strings.
QStringListModel::~QStringListModel()
Destroys the model.
bool QStringListModel::clearItemData(const QModelIndex &index)
Reimplements QAbstractListModel::clearItemData().
Makes the string at index empty.
This function was introduced in 6.0.
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.
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.
bool QStringListModel::insertRows(int row, int count, const QModelIndex &parent = QModelIndex())
Reimplements QAbstractListModel::insertRows().
Inserts count empty strings before row. parent must be invalid.
QMap<int, QVariant> QStringListModel::itemData(const QModelIndex &index) const
Reimplements QAbstractListModel::itemData().
Returns the display and edit data of the item at index.
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.
bool QStringListModel::removeRows(int row, int count, const QModelIndex &parent = QModelIndex())
Reimplements QAbstractListModel::removeRows().
Removes count strings from row. parent must be invalid.
int QStringListModel::rowCount(const QModelIndex &parent = QModelIndex()) const
Reimplements QAbstractListModel::rowCount().
Returns the number of strings, or 0 for a valid parent.
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.
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().
QModelIndex QStringListModel::sibling(int row, int column, const QModelIndex &idx) const
Reimplements QAbstractListModel::sibling().
Returns the index at row and column next to idx.
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().
Qt::DropActions QStringListModel::supportedDropActions() const
Reimplements QAbstractListModel::supportedDropActions().
Returns Qt::CopyAction and Qt::MoveAction.