Results 1 to 7 of 7

Thread: Custom delegate and event management

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Custom delegate and event management

    Guys, don't overcomplicate things. This is not a task for a delegate, it's a task for the view or its environment. The delegate is responsible for handling one item only, it doesn't interconnect items together in any way. What you want is identical to the Qt::CheckState role in Qt's models. Provide an identical mechanism (involving all the view, delegate and the model) if you can't reuse the check state one. Then connect to the dataChanged() signal in the model (or a custom signal you will emit), track that the change was in this particular role and update the view accordingly. Alternatively implement that directly in the view, without touching the model (like the branching mechanism in QTreeView is implemented) - that would be an even better solution.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #2
    Join Date
    Aug 2009
    Posts
    23
    Thanks
    3

    Default Re: Custom delegate and event management

    Thank you all for your time and advices,

    Wysota, you point my problem! I understood that my delegate must handle only one item so I didn't know how to manage actions on all selected items.
    I'll go with your solution because it looks to be the natural way to do.

    As I already enable/disable the icons depending on boolean values in the model, I guess I could solve my problem only by connecting the QAbstractItemModel::dataChanged() signal.
    When I am notified, I can retrieve the selected items, check if the current item is part of the selection and then modified all selected items if so.

    I'll keep you informed ...

    Regards,
    Lionel

  3. #3
    Join Date
    Aug 2009
    Posts
    23
    Thanks
    3

    Default Re: Custom delegate and event management

    It brings another problem ... I have 4 TreeViews on 1 Model so I get notified 4 times on dataChanged().
    First solution I see is to keep an internal flag to know when the user has really made a click. I can do it by using the mousePressEvent().
    I think there is a better way to solve it.


    The other problem I see is that I'll get notified on dataChanged(), then I'll update all selected items then get notified again ... and it will be an infinite loop.

    Grrrr....

    Regards,
    Lionel

  4. #4
    Join Date
    Aug 2009
    Posts
    23
    Thanks
    3

    Default Re: Custom delegate and event management

    I made it work ... however I am not sure it's a pretty solution.

    1/ On the TreeView, I override QAbstractItemView::mouseReleaseEvent() so I can retrieve the list of selected rows. I give this list to the delegate then I call QAbstractItemView::mouseReleaseEvent() on the parent class.

    2/ In the delegate class, I override QAbstractItemDelegate::editorEvent(). For the QEvent::MouseButtonRelease event, I check if the click hits an icon and then call a method itemClicked() on my proxy model with the given index and the list of selected rows I previously stored.

    3/ In the method itemClicked() of the proxy model, I check if the index is part of the selection and update the value of the given index and all selected items.

    I ran a few tests and it looks like it works but I need more tests to be sure.
    I was also thinking about giving the list of selected rows directly to the proxy model so my delegate would only check the position of the mouse and then let the proxy model work on all selected items.

    I still have a question if you don't mind ... When hitting an icons, I would like to keep the selection. For example, I have 3 items (1, 2, 3), I select 1 & 2 and I hit an icon on the 3rd item, I would like to keep 1 & 2 as the selected items.
    I tried to do it in the QAbstractItemDelegate::editorEvent() and it works when I hit an icon for a non-selected item. But when I hit an icon for an item which is a part of the selection, this particular item is the only one selected afterwards.

    Regards,
    Lionel

Similar Threads

  1. Mouse Leave and Tab Key event in Delegate
    By faldzip in forum Qt Programming
    Replies: 0
    Last Post: 30th October 2008, 19:04
  2. Capture events in a custom delegate
    By vfernandez in forum Qt Programming
    Replies: 9
    Last Post: 19th March 2008, 05:33

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.