Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. ListView with shift/ctrl-based multiple selection
Forum Updated to NodeBB v4.3 + New Features

ListView with shift/ctrl-based multiple selection

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 1 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bob64
    wrote on last edited by
    #1

    I am trying to introduce some 'standard' multiselect paradigms into an existing ListView-based component - i.e., so that shift+leftMouse selects from previous selection to current position, control+leftMouse toggles the selection of the current item without clearing existing selections, etc.

    I happened across DelegateModel and DelegateModelGroup and wondered if this might help. In fact the example in the documentation of the DelegateModel.group property looked similar to what I would need for the control+select case but shift selection is less obvious - I would need to add entries into the group that represent the implied selected range and it's not obvious to me how to do that.

    I suspect I am barking up the wrong tree with DelegateModelGroup. I have also just come across ItemSelectionModel and am about to have a play with that to see if I can build something on that. There seems to be surprisingly little information out there about multi-select and ListView so I thought it might be worth asking here to see if anyone has any experience of implementing something similar, or has any ideas or suggestions.

    Thanks.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bob64
      wrote on last edited by
      #2

      Well, it looks like ItemSelectionModel could be useful but I have to say that the documentation is pretty rubbish. In any event, it is not working as I would have expected it to.

      The following is all I am doing to introduce it at the moment:

          ItemSelectionModel {
            id: selectionModel
            model: myModel
          }
          ...
      
                      MouseArea {
                          ...
                          onClicked: {
                              if (mouse.modifiers & Qt.ControlModifier) {
                                  console.log("selecting ", myModel.data(myModel.index(index, 0)).value, index)
                                  selectionModel.select(myModel.index(index, 0), ItemSelectionModel.SelectCurrent);
                                  console.log("selected indexes", selectionModel.selectedIndexes, 
                                              "length=", selectionModel.selectedIndexes.length)
                              }
                          }
                      }
      

      ItemSelectionModel.SelectCurrent is supposed to be equivalent to ItemSelectionModel.Select | ItemSelectionModel.Current but I have tried both. The way this is supposed to work as I understand it is that each selected index should be added to the current selection but I only ever see one selection in selectedIndexes in the output above.

      Please can someone suggest where I am going wrong here? Thanks.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Bob64
        wrote on last edited by
        #3

        It looks like I was misunderstanding what was meant by "current selection". From the docs for QItemSelectionModel:

        "The QItemSelectionModel takes a two layer approach to selection management, dealing with both selected items that have been committed and items that are part of the current selection. The current selected items are part of the current interactive selection (for example with rubber-band selection or keyboard-shift selections)."

        Using the Current flag causes a new current selection to be created with the new item, throwing away the other one. Avoiding Current causes the selected item to be added to the committed selection, which is what I was expecting.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved