Important: Please read the Qt Code of Conduct -

Capture item selection via mouse of treeview?

  • 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?

  • Moderators

    Should I derive my plain class items from QAbstractItemView

    No, it's a view class, not an item class. QTreeView is 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.

Log in to reply