I have a pointer to a third party QListView
object, which is simply displaying rows of text. What is the best way of getting a hold of that string of text?
The model, accessible by QListView::model()
, holds the items. You can do something like this:
QListView* view ; // The view of interest
QAbstractItemModel* model = view->model() ;
QStringList strings ;
for ( int i = 0 ; i < model->rowCount() ; ++i )
{
// Get item at row i, col 0.
strings << model->index( i, 0 ).data( Qt::DisplayRole ).toString() ;
}
You also mention you would like to obtain the updated strings when text is written - you can do this by connecting the model's dataChanged()
signal to your function that extracts strings. See QAbstractItemModel::dataChanged()
.
You can ask the QListView object for its root QModelIndex and use that to iterate over the different entries using the sibling/children methods. You can access the text associated with each index by calling the data method on the index with the role specified as the Qt::DisplayRole.
For more details see the following documentation:
QAbstractItemView - parent class to QListView
QModelIndex
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With