Results 1 to 14 of 14

Thread: setSelection() in custom view

  1. #1
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  3. #3
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: setSelection() in custom view

    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.
    J-P Nurmi

  5. #5
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: setSelection() in custom view

    Quote Originally Posted by jpn View Post
    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..

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: setSelection() in custom view

    Quote Originally Posted by krudio View Post
    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.
    J-P Nurmi

  7. #7
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: setSelection() in custom view

    Quote Originally Posted by jpn View Post
    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()?

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: setSelection() in custom view

    Quote Originally Posted by krudio View Post
    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().
    J-P Nurmi

  9. #9
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: setSelection() in custom view

    Quote Originally Posted by jpn View Post
    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?

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: setSelection() in custom view

    No, setSelection() is fine. What's the problem with it?
    J-P Nurmi

  11. #11
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: setSelection() in custom view

    Quote Originally Posted by jpn View Post
    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?

  12. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: setSelection() in custom view

    Quote Originally Posted by krudio View Post
    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).
    J-P Nurmi

  13. #13
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: setSelection() in custom view

    Quote Originally Posted by jpn View Post
    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?

  14. #14
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: setSelection() in custom view

    My guess is that "indexAt(rect.center())" is suitable for you.
    J-P Nurmi

  15. The following user says thank you to jpn for this useful post:

    krudio (6th February 2008)

Similar Threads

  1. Model-view: Creating a custom view
    By taboom in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2007, 20:36
  2. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 08:50
  3. Custom widget in list view
    By fusoin23 in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2006, 14:09
  4. Replies: 1
    Last Post: 13th July 2006, 21:10
  5. Example HowTo create custom view
    By dexjam in forum Newbie
    Replies: 6
    Last Post: 12th July 2006, 11:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.