3

setCurrentItem only sets one item selected. I don't see any method to set more than 1 item selected programmatically, but maybe I'm overlooking something?

Of course, my tree widget is configured to enable multiple selection.

Note that I'm using QTreeWidget, not QTreeView.

Kumar V
  • 8,810
  • 9
  • 39
  • 58
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335

2 Answers2

5

Use setSelectionMode:

treeWidget->setSelectionMode(QAbstractItemView::MultiSelection);

And on the items you can use setSelected:

treeWidgetItem->setSelected(true);
Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
Trashed
  • 146
  • 3
2

Yes, you use the selection model:

QModelIndex index = ...; // index you want to select.
QItemSelectionModel* sel_model = tree_view->selectionModel();
sel_model->select(index, QItemSelectionModel::Select);

There are other ways of manipulating the selection mode - see the Qt Assistant for more details.

Pete
  • 4,784
  • 26
  • 33