PDA

View Full Version : QT4 - get content of a QAbstractItemModel



incapacitant
17th March 2006, 18:37
I have this piece of code that I found on the forum :


QListView *lv; // I use a QListView to view my QAbstractItemModel
...
QItemSelectionModel *sel = lv->selectionModel();
bool third = sel->isRowSelected(3, lv->rootIndex()); // check if row "3" is selected
QModelIndexList list = sel->selectedIndexes();
foreach(QModelIndex index, list){
qDebug("Row %d selected", index.row());
}


How can adapt it to read the content of the model row by row without foreach ?
In fact I need the contents of rows 3,4, up to rowCount().

Could I do foreach and have :


foreach(QModelIndex index, list){
if (index.row() > 2) qDebug("Row %d selected", index.row());
}

But how do I set the index to contain all rows (selected or not) ?
And how can I get the actual value for each index ?

wysota
17th March 2006, 18:46
QAbstractItemModel::index() and QAbstractItemModel::data().

incapacitant
17th March 2006, 19:05
I can't get it right :


for (int row = 3; row < qrecap_model->rowCount(); row++)
{
QModelIndex idx = (qrecap_model->index(row, 0));
QString s = qrecap_model->data(idx); <- this does not compile
}

236 C:\Qt\test\sms\menu.cpp no matching function for call to `QAbstractItemModel::data(QModelIndex**)'

A bit of help ?

wysota
17th March 2006, 23:47
Surely you have to convert QVariant, which gets returned by the model to QString by using QVariant::toString(). But I'm wondering where does this "QModelIndex**" come from. Could you show a larger portion of your code?

incapacitant
18th March 2006, 07:23
if ( qrecap_model->rowCount() > 3 )
{
for (int row = 3; row < qrecap_model->rowCount(); row++)
{
QModelIndex idx = (qrecap_model->index(row, 0));
s = qrecap_model->data(idx).toString();
}
}


adding toString() has made it compile ?

Anyway big thanks