PDA

View Full Version : setSelection() in custom view



krudio
5th February 2008, 21:13
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

marcel
5th February 2008, 21:16
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.

krudio
5th February 2008, 21:25
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

jpn
5th February 2008, 21:43
As docs say:


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. ;)

krudio
5th February 2008, 21:49
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..

jpn
6th February 2008, 08:26
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.

krudio
6th February 2008, 18:49
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()?

jpn
6th February 2008, 18:53
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().

krudio
6th February 2008, 18:59
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?

jpn
6th February 2008, 19:04
No, setSelection() is fine. What's the problem with it? :)

krudio
6th February 2008, 19:11
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?

jpn
6th February 2008, 19:34
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).

krudio
6th February 2008, 19:39
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? :)

jpn
6th February 2008, 20:03
My guess is that "indexAt(rect.center())" is suitable for you.