Retrieving Text from a QListView

I have a pointer to a third-party object QListViewthat just displays lines of text. What is the best way to get this line of text?

+5
source share
2 answers

The model available QListView::model()contains the elements. 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 note that you want to get updated lines when writing text - you can do this by connecting the model signal dataChanged()to your function, which extracts the lines. See QAbstractItemModel::dataChanged().

+4
source

QListView QModelIndex sibling/children. , , , Qt:: DisplayRole.

. :

QAbstractItemView - QListView

QModelIndex

+3

All Articles