View Full Version : QTableWidget - Select multiple rows.

17th October 2008, 19:37

I am looking to select multiple rows in a tablewidget at once, as if the user had Ctrl+clicked or Shift-clicked on the vertical headers.

I have the selection mode set to QAbstractItemView::ExtendedSelection and can perform this operation by clicking.

Using selectRow(int) clears the previous selection. Looking through the source code, selectRow in QTableView calls QTableViewPrivate::selectRow(int, bool).

My guess is that I would have to reimplement QTableView::selectRow() but I'm not quite sure how to do that to get the behaviour I want. I'm working on it in the mean time but if anyone has any suggestions that would be great.

I am currently using Python and PyQt4 for most of my development but I am comfortable with C++ as well.

17th October 2008, 22:11
Hello Everyone,

Turns out I figured out how to do what I want. I don't know if this is the most elegant way to perform this action but it works. I'm just going to put the idea into a method called "selectRows" in my subclassed table.

Turns out my problem was using QItemSelectionModel::Rows instead of QItemSelectionModel::Select.

#include <QtGui/QApplication>
#include <QtGui/QTableWidget>
#include <QtGui/QAbstractItemView>

int main(int argc, char *argv[])
QApplication a(argc, argv);
QTableWidget w(10,3);
QItemSelectionModel *selectionModel = w.selectionModel();
QItemSelection itemSelection = selectionModel->selection();
itemSelection.merge(selectionModel->selection(), QItemSelectionModel::Select);
itemSelection.merge(selectionModel->selection(), QItemSelectionModel::Select);

w.show(); // ideally, upon show rows 2,4 and 6 should appear selected.
// now, all three rows are selected.
return a.exec();

21st October 2008, 18:15
what about using the designer go to the properties of the QtableWidget
and for selectionbehavior , SelectRows is active.