setSelection() in custom view
Hello,
I have created a custom hierarchical view (derived from QAbstractItemView) that uses custom widgets to represent each item. The view works fine, however, I now need to be able to support multiple selections within the view. My question is, how do I re-implement setSelection() to support this custom view? I know that it takes a QRect as a parameter, but I'm not quite sure what I'm supposed to do with that QRect
Re: setSelection() in custom view
Well, we don't know how you created the view.
Selection in views is handled by a QItemSelectionModel.
So you should take a look at that.
Re: setSelection() in custom view
I created the view by deriving from QAbstractItemView, and setSelection() is one of the pure virtual methods that I must re-implement in my own view. I am aware that each view has its own QItemSelectionModel, but my understanding was that setSelection() was the method that actually passes selected items to the QItemSelectionModel
Re: setSelection() in custom view
As docs say:
Quote:
Applies the selection flags to the items in or touched by the rectangle, rect.
When implementing your own itemview setSelection should call selectionModel()->select(selection, flags) where selection is either an empty QModelIndex or a QItemSelection that contains all items that are contained in rect.
QAbstractItemView sorts out proper flags for you. All you need to do is to determine which items are effected by the selection area, rect. I'd take a sneak peek to QTreeView/QListView/QTableView sources for implementation tips. ;)
Re: setSelection() in custom view
Quote:
Originally Posted by
jpn
As docs say:
QAbstractItemView sorts out proper flags for you. All you need to do is to determine which items are effected by the selection area, rect. I'd take a sneak peek to QTreeView/QListView/QTableView sources for implementation tips. ;)
That clears things up a bit, but I'm still confused as to what the selection rect is? Is that equivalent to the rectangle that appears when you click and drag to select a group of icons on a desktop, for example? If so, we are not supporting that kind of functionality in our custom view..
Re: setSelection() in custom view
Quote:
Originally Posted by
krudio
I'm still confused as to what the selection rect is? Is that equivalent to the rectangle that appears when you click and drag to select a group of icons on a desktop, for example? If so, we are not supporting that kind of functionality in our custom view..
Yes, it's mainly that. In addition to that, it is also used to make the "current item" selected while navigating with keyboard.
Re: setSelection() in custom view
Quote:
Originally Posted by
jpn
Yes, it's mainly that. In addition to that, it is also used to make the "current item" selected while navigating with keyboard.
Ok, that makes sense. So which method then is responsible for selecting an item when a user clicks on it with the mouse? Is it visualRegionForSelection()?
Re: setSelection() in custom view
Quote:
Originally Posted by
krudio
Ok, that makes sense. So which method then is responsible for selecting an item when a user clicks on it with the mouse? Is it visualRegionForSelection()?
QAbstractItemView::setSelection() is called from both, QAbstractItemView::mousePressEvent() and QAbstractItemView::mouseMoveEvent().
Re: setSelection() in custom view
Quote:
Originally Posted by
jpn
QAbstractItemView::setSelection() is called from both, QAbstractItemView::mousePressEvent() and QAbstractItemView::mouseMoveEvent().
So should I override mousePressEvent() and mouseMoveEvent() in my custom view to support the selection of my custom items?
Re: setSelection() in custom view
No, setSelection() is fine. What's the problem with it? :)
Re: setSelection() in custom view
Quote:
Originally Posted by
jpn
No, setSelection() is fine. What's the problem with it? :)
Well, I guess my problem is that I do not need to support selection with a rectangle or with the keyboard. I only want items to be selected when a user clicks on them with the mouse. Unfortunately, setSelection() only takes a QRect and a SelectionFlag as parameters. What is your recommendation in this case?
Re: setSelection() in custom view
Quote:
Originally Posted by
krudio
Well, I guess my problem is that I do not need to support selection with a rectangle or with the keyboard. I only want items to be selected when a user clicks on them with the mouse. Unfortunately, setSelection() only takes a QRect and a SelectionFlag as parameters. What is your recommendation in this case?
Well, I'd implement the selection like the framework expects you to, with setSelection(). Notice that there are a few built-in properties to adjust the way selection works (namely selection behavior and mode). These properties are properly taken into consideration in various methods of QAbstractItemView which further call setSelection(). So I'd use setSelection() instead of reimplementing the selection from scratch to a certain event handler. You can later restrict the ways of selection if really required, for example with empty event handler reimplementation(s).
Re: setSelection() in custom view
Quote:
Originally Posted by
jpn
Well, I'd implement the selection like the framework expects you to, with setSelection(). Notice that there are a few built-in properties to adjust the way selection works (namely selection behavior and mode). These properties are properly taken into consideration in various methods of QAbstractItemView which further call setSelection(). So I'd use setSelection() instead of reimplementing the selection from scratch to a certain event handler. You can later restrict the ways of selection if really required, for example with empty event handler reimplementation(s).
Ok, I have the selection behavior and mode flags set the way I want them, but I'm still confused on how to re-implement setSelection() for what I need it to do. Since the method is not passed a QPoint or QModelIndex, I have no way to tell which item the user clicked on. Do you see my dilemma here? :)
Re: setSelection() in custom view
My guess is that "indexAt(rect.center())" is suitable for you.