Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining text from a QListView

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?

like image 613
David Menard Avatar asked Jul 28 '09 19:07

David Menard


2 Answers

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().

like image 182
swongu Avatar answered Sep 18 '22 06:09

swongu


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

like image 39
Joe Corkery Avatar answered Sep 18 '22 06:09

Joe Corkery