PDA

View Full Version : iterating selected rows in a qtableview



JeanC
19th January 2008, 12:55
I want to act on the selected rows in a qtableview.

I'm trying the code below, however, list.count() gives the amount of selected cells in stead of selected rows, I only need the rowcount. How do I get that please, I searched but seems I'm too dumb to find it.



const QModelIndexList list = table->selectionModel()->selection().indexes();
for (int i = 0; i < list.count(); i++)
{
QModelIndex index = list.at(i);
int row = index.row();
QString name = model->record(row).value("name").toString();
qDebug() << i << row << name;
}
}

jpn
19th January 2008, 13:24
If you used "select rows" selection behavior you could simply use QItemSelectionModel::selectedRows(). If not, you could use for example a set:



QSet<int> rows;
const QModelIndexList list = table->selectionModel()->selection().indexes();
for (int i = 0; i < list.count(); i++)
{
QModelIndex index = list.at(i);
rows.insert(index.row());
}
qDebug() << rows;

JeanC
19th January 2008, 15:29
If you used "select rows" selection behavior you could simply use QItemSelectionModel::selectedRows(). If not, you could use for example a set:

I have that behaviour yes, gee why didn't I see that. :)




QSet<int> rows;
const QModelIndexList list = table->selectionModel()->selection().indexes();
for (int i = 0; i < list.count(); i++)
{
QModelIndex index = list.at(i);
rows.insert(index.row());
}
qDebug() << rows;


Thanks for both.