Search code examples
stringqttextqlistview

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?


Solution

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