views:

596

answers:

2

Hey, I have a pointer to a third party QListView object, wich is simply displaying rows of text. What is the best way of getting a hold of that string of text?

thanks, Dave

+3  A: 

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

Joe Corkery
and http://doc.trolltech.com/4.5/qvariant.html#toString
thanks, ill try it. Is there a slot I can connect to to do this when new text is written to it?
David Menard
+1  A: 

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

swongu
is this right?QObject::connect(model, SIGNAL(dataChanged (QModelIndex,QModelIndex) ), client_, SLOT(onText()) )where client_ is a class deriving from QObject, and onText is declared under public slots.
David Menard
Yes, this is the idea. If your onText() signature also matches the dataChanged() ones, you'll be able to loop only through the indices on which the data changed, rather than the entire list.
swongu
this is now my line:QObject::connect(model, SIGNAL(dataChanged (const QModelIndex , const QModelIndex ) ),client_,SLOT(onText(const QModelIndex , const QModelIndex )) );it returns true, but I don't see the cout I put in the "onText" function. Any ideas?
David Menard
and thanks for the quick answers, getting the text from the QListView works fine
David Menard