Important: Please read the Qt Code of Conduct -

Custom view - problems with selection

  • Hi

    I've a custom view which is meant to be used with list-like models (one column, n-rows).
    My view displays items horizontaly, but wraps when right edge is reached. Here is an example:

    Consider model with 10 items (one item per row in 1 column).
    My view would display them like that:

    | Item1   Item2   Item3   Item4   |
    | Item5   Item6   Item7   Item8   |
    | Item9   Item10                  |
    |                                 |

    I hope it is clear. The problem I have is selection. I wish I could have the bahavior like that: when user clicks Item2 and then clicks Item6 with shift, items 2, 3, 4, 5 and 6 should be selected. However Qt framework calls setSelection() in my view with a rect which covers Item2 and Item6. In a general case I'm unable to figure out which items should I pass to selection model to get my desired behavior. I went through QAbstractItemView's doc and I cannot see anything that could help me. Any ideas or suggestions?

  • Lifetime Qt Champion


    What widget are you using for that ?

  • I've a custom class for this view derived directly from QAbstractItemView.

  • Lifetime Qt Champion

    Then it would likely be nice to share the code you are using for the view.

  • @SGaist
    here you go: images tree view. In line 249 (const std::deque<QModelIndex> linear_selection = convertToLinearSelection(items);) I was trying to convert rect into something that suits me, but it is working only under certain circumstances.

    However I think that code is irrelevant here. I just have very untypical view which arranges items in different way they are stored in model. Qt framework sends me selection as a rect which doesn't allow me to select items the way I want.

  • Lifetime Qt Champion

    Did you already saw the using a selection model part of the Model View Programming chapter ?
    There's also the Model View tutorial that could be of interest.

  • @SGaist Thank you for your interest however I'm afraid we do not understand each other.
    I'll deal with my problem by overriding QWidget::mousePressEvent and doing my own clicks interpretation there to generate proper selection.

    If you would like to see visualtion of my problem you can compile example which also suffers from the same problem as I do.
    Select with rectangle any two consecutive elements and only these two will get selected.
    One exception from this rule: select biggest blue item and the green one above it. Whole chart will be selected. This is because Qt framework sends selection as a rect which covers first and last item and in my case I'm unable to figure out which two items were actualy selected.


Log in to reply