I found multiple answers on how to do it from C++, but not from QML.
How is it possible to get a specific (based on index) row from QStringListModel? I tried expressions that worked from other models, but it did not work for QStringListModel. I also tried to use
var dataRow = myModel.data(rowNumber)
But it returned "undefined".
When you call myModel.data
you are calling QVariant QAbstractItemModel::data(const QModelIndex &index, int role = Qt::DisplayRole)
.
This methods takes a QModelIndex
and an optional int for the role.
The display role is what you want when you query a QStringListModel
so you don't need to specify the role parameter.
However you do need to specify the index parameter with a valid QModelIndex
. You can get one from the model with QModelIndex QAbstractItemModel::index(int row, int column, const QModelIndex &parent = QModelIndex()) const
So in your case the correct way to do it would be :
var dataRow = myModel.data(myModel.index(rowNumber, 0));
You can call data
and index
from QML because both are Q_INVOKABLE
.