Petr
Petr

Reputation: 14505

how can I get indexes of multiple selected rows in QTableWidget

I have a table where user can select multiple rows, however I need to know indexes of top and last selected row, I tried playing with http://qt-project.org/doc/qt-5/QModelIndex.html so far I have this:

QItemSelectionModel *selections = this->ui->tableWidget->selectionModel();
QModelIndexList selected = selections->selectedRows(3);

But I have no idea how can I use QItemSelectionModel to reach the item of table. How can I do that? There is no function in TableWidget that returns item based on QModelIndex, only QPoint

Upvotes: 0

Views: 2786

Answers (2)

vahancho
vahancho

Reputation: 21250

In order to get the first and last item in the selection range you can simply sort that list. For example:

QItemSelectionModel *selections = this->ui->tableWidget->selectionModel();
QModelIndexList selected = selections->selectedRows(3);
qSort(selected);
QModelIndex first = selected.first();
QModelIndex last = selected.last();

And now let's get the first and last table items:

QTableWidgetItem *firstItem = this->ui->tableWidget->item(first.row(), first.column());
QTableWidgetItem *lastItem = this->ui->tableWidget->item(last.row(), last.column());

Upvotes: 1

Erik
Erik

Reputation: 2207

Is QTableWidget::item(int row, int column) together with QModelIndex::column () and QModelIndex::row (), respectively, of any help?

Upvotes: 1

Related Questions