Results 1 to 10 of 10

Thread: Reusing the editing widget with QtItemDelegate

  1. #1
    Join Date
    May 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Reusing the editing widget with QtItemDelegate

    Is there a way to prevent the view from destroying the widget returned from QItemDelegate::createEditor() ?
    I would like to reuse this widget in subsequent calls if possible.

    Thanks
    Bruce

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Reusing the editing widget with QtItemDelegate

    I guess you need to create a subclassed item delegate and reimplement the createEditor function.

    Why do you want to reuse the widget?

  3. #3
    Join Date
    May 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reusing the editing widget with QtItemDelegate

    I have created a subclass of QItemDelegate that returns a widget from createEditor, but this widget is destroyed when the in cell editing has completed.

    My reason for wanting to retain and reuse the widget is to do with the application framework that we use, it would be much easier to reuse the widget, than having to recreate it all the time. Also the user is likely to do a lot of in cell editing within this application, so reuse makes sense.

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reusing the editing widget with QtItemDelegate

    But at a time you will be editing only a single cell, right ?
    So using a editor till you are editing, and then destroying it makes sense. What if the user never enters the edit mode ?

    Still if you want to use a permanent widget for editor, you can create a singleton of the editor widget, and return instance of this widget from within the createEditor .

  5. #5
    Join Date
    May 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reusing the editing widget with QtItemDelegate

    Please explain (sample code?) how a singleton would prevent the deletion of the widget by the view?

    I have just found one way to circumvent the destruction, by installing an event filter on the widget and rejecting QEvent DeferredDelete

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Reusing the editing widget with QtItemDelegate

    Quote Originally Posted by bcollie View Post
    Please explain (sample code?) how a singleton would prevent the deletion of the widget by the view?
    You keep a list of editors in your subclassed delegate (for each item or cell). When the widget already exists return the existing one, if not, create a new one.

    I have just found one way to circumvent the destruction, by installing an event filter on the widget and rejecting QEvent DeferredDelete
    This maybe stops the deleting, but it doesn't stop the delegate from returning a new widget.

  7. #7
    Join Date
    May 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reusing the editing widget with QtItemDelegate

    Ok I must be missing something here, from the example code I have, any widget returned from createEditor will be destroyed by the view once the in cell editing has finished, so retaining a reference to the widget within the delegate will end up returning a bad pointer the next time createEditor is called.

    Adding the event filter is the only way I have found to prevent this.

  8. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Reusing the editing widget with QtItemDelegate

    Yes, you'll need to prevent the deletion.
    If this needs to be done with an event, I don't know.

    One thing to do is look at the source code of QItemDelegate. This can be complex.
    Therefor, it might be better to start with an abstract item delegate, then you have a lot more control (as in: less code to change).

  9. #9
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Reusing the editing widget with QtItemDelegate

    In general, trying to do end runs around complex systems of objects - like Qt's Model/View/Delegate system - is a bad idea. Yes, you can probably find a solution. But your code will be significantly more complex and difficult to maintain than otherwise.

    Creating/deleting delegates is a reasonable time/memory tradeoff in nearly all cases. Consider that each and every cell in a table can potentially contain its own custom delegate; now, you potentially have thousands of delegate objects slumming around, with only one of them ever active at any given time - a huge waste of memory. Meanwhile, creation/deletion of a specialized delegate is not typically going to require much overhead, and it keeps as much memory freed up as possible.

    Unless your delegate creation takes a significant amount of time, I'd work within Qt's framework and just allow your delegate to be created and deleted by the view.

    If you really can't do this, you're going to have to add some sort of smart pointer with reference counting to your singleton and overload the delete() operator. Note that this sort of thing, while feasible, gets very tricky very quickly.

  10. #10
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reusing the editing widget with QtItemDelegate

    I have just found one way to circumvent the destruction, by installing an event filter on the widget and rejecting QEvent DeferredDelete
    Yeps.. sorry I forgot about the deletion case..

    but I guess you can try the following - am not sure if its a good solution.

    in createEditor() emit a signal(editingRequired()) that editing widget needs to be displayed. Return 0 from this function.
    connect editingRequired() to some slot of your delegate... pass some info if required about where to show the editor, etc.
    Now in the slot for editingRequired() ,you can use the singleton concept.

    Since you return 0 from createEditor deletion wont be called.. and still you have a way to use the singleton.


    Still I would suggest let it be the way it is, dont think creating / deleting editors are that heavy. Give it a try though

Similar Threads

  1. Midi editing widget
    By dentist in forum Newbie
    Replies: 1
    Last Post: 24th March 2010, 23:07
  2. Reusing a QPainterPath
    By Micawber in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2009, 19:49
  3. QtItemDelegate for progress bars
    By maxpayne in forum Qt Programming
    Replies: 2
    Last Post: 19th November 2008, 13:26
  4. Replies: 3
    Last Post: 2nd August 2007, 21:28
  5. Reusing .ts without source
    By langevin in forum Qt Programming
    Replies: 2
    Last Post: 21st December 2006, 17:57

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.