开发者

Setting multiple selection in QTreeView

开发者 https://www.devze.com 2023-02-13 03:13 出处:网络
I have 开发者_Go百科a need here regarding multiple selection of items in the QTreeView. I have two widgets, QTreeView on left and another QGLWidget on the right. If I do a multiple selection, I must h

I have 开发者_Go百科a need here regarding multiple selection of items in the QTreeView. I have two widgets, QTreeView on left and another QGLWidget on the right. If I do a multiple selection, I must highlight the items in the glwidget. Vice versa, I need to highlight the items in the tree view if multiple selection is done on the glwidget. Currently, I am able to update single item by setting the current index of the tree view. Any ideas on how to update the selection of multiple items on the tree view with multiple selection on glwidget?


You can use the tree view's item selection model (treeView->selectionMode(), of type QItemSelectionModel). It has a signal selectionChanged() you can connect to to receive tree view selection changes and apply them to the GL view. When receiving selection events from the GL view, you can use QItemSelectionModel::select() to propagate them to the tree view.

To enable multiselection on your treeview, call setSelectionMode( MultiSelection ).


Frank was faster, but I still post my (untested) code example for adding an item to the current selection:

treeView->selectionModel()->select(
    treeView->model()->index(row, column, parent), 
    QItemSelectionModel::SelectCurrent);

There are other selection modes too, see the QItemSelectionModel reference. SelectCurrent is a short-hand for Select | Current, so means update current selection by selecting the given items. It does not mean "select as current selection" (replacing previous selection).


In Python (PyQt6) I do :

ui.my_treeView.setSelectionMode(ui.my_treeView.selectionMode().MultiSelection)

It works well.

0

精彩评论

暂无评论...
验证码 换一张
取 消