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.

有帮助吗?

解决方案

Use setSelectionMode:

treeWidget->setSelectionMode(QAbstractItemView::MultiSelection);

And on the items you can use setSelected:

treeWidgetItem->setSelected(true);

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top