ItemView with additional selection rules

  • Hi,

    i am using a QTreeView for my ItemModel. I would like to have additional rules concerning the selection of items in the view.

    If a child node (depth 1) of root is selected all other nodes should become deselected.

    If more than one item with a depth >= 2 is selected the items have to be children of the same parent. Otherwise the last selection is not processed.

    I have not found a good way yet.

    Currently i inherit from QItemSelectionModel and QItemSelectionModelPrivate to accomplish that goal. There i override the select method.

    But that solution is not satisfying (Qt compability + ugly workaround).

    Is there another way to realize that?

    Best regards

  • Moderators

    in 99% of all cases - unless you know hwat you are doing ;) - you shouldn't subclass a private class of Qt. Since they are only meant to be used internally by Qt.

    You should be fin subclassing QItemSelectionModel and reimplementing it's select method().

    But do you really need a selection? I mean when you just want to show the user that some nodes are logically connected you should use a custom delegate and paint set a different background color. Or do you depend on a item selection?

  • Thx for your response.


    in addition i have a graphicsScene. The nodes under the root are project nodes. Every project has its own scene. The items under the project nodes are connected with graphicsItems through the model.

    Switching a project node or a child is switching the scene in the graphicsView and selecting the graphicsItem. Selecting a graphicsItem in a scene is selecting the corresponding item in the treeView.

    I want multiselection to be possible in both ways.

    The way from grahicsScene to itemView is ok.
    But selecting nodes in the itemview which are related to different project nodes is causing problems.

    There is no crash or something - the problem is that the selection does not fit (visually) to the selection in the graphicsView.

    So this rules have to work while the selection takes place and not after that.

    The existing implementation of the selection method is working with the selection ranges of the itemView which are behind the d-pointer. The old ranges are importent for the selectionChanged signal (the implementation is also doing some other important stuff).

    I could call the original select out of my own implementation of select. But that wouldn't give my own implementation the power it needs.

    I hope this gives a better idea of the problem i have.

  • It seems that i was impatient again.

    I was not watching the SelectionFlag paremeter of the select method thoroughly.

    Playing with it i could see that following combinations of flags are given by this parameter.

    Simple Click on item results in:
    QItemSelectionModel::Clear and

    Using the shift key while selection result in:
    QItemSelectionModel::Select and

    Using Ctrl while clicking an UNselected item results in:

    Using Ctrl while clicking an selected item results in:

    So i can take a look at the already selected items by QItemSelectionModel::selectedIndexes in my own select implementation.

    I can check if the newly selected items have the same parent or are children of root etc.

    If the selection would break my rules i simply return.

    And RIGHT NOW (while writing this) i relaize that i did not need the flags for that!

    Aiaiai ... brain, c'mon!

    Thx for your help anyway. If someone has other suggestions to realize such rules ... still welcome.

    I hope my brain is telling me the truth this time. I will report if everything is working as expected.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.