Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Capture item selection via mouse of treeview?
SRaD last edited by
I am using a QTreeView where I set a custom model which is derived from QAbstractItemModel and I fill the tree with several plain classes for the tree items.
I am trying to capture the selection by mouse of a tree item within my TreeView.
I saw QAbstractItemView class has a clicked signal. Should I derive my plain class items from QAbstractItemView in order to capture a tree item selection or is there another way I should do this?
Should I derive my plain class items from QAbstractItemView
No, it's a view class, not an item class.
QTreeViewis a subclass of it. Selection is a function of a view. Selection is handled by a selection model set on a view. You should not tie selection to your model items.
If you want to change the items in some way in response to selection change you can get the selection model from your tree view with selectionModel() and connect to its selectionChanged() signal. In the slot connected to it you can call a function on the model that will do something with the selected items, e.g. setData() with a custom role.