Results 1 to 8 of 8

Thread: Best way for a graphicsitem to delete itself

  1. #1
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Best way for a graphicsitem to delete itself

    I have some graphicsitems that need to delete themselves once they're no longer needed. Right now I have a fadeTo(float) function, which will set up an animation to fade the item to a particular opacity value.
    If I go item->fadeTo(0), it will fade to transparent and then delete itself, like so:

    Qt Code:
    1. void SongItem::onAnimationFinished() {
    2. onAnimationValueChanged(1.0);
    3. animationTimeline->disconnect(this);
    4. if(!opacity)
    5. delete this;
    6. }
    To copy to clipboard, switch view to plain text mode 

    That function is hooked up to the finished() signal of the timeline.

    So my question is: Is having an item delete itself in a slot safe? If not, what's the best way to do it?

    Thanks,
    leo

  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: Best way for a graphicsitem to delete itself

    You could implement a simple garbage collector in a subclassed graphics view.
    Create a base graphics item from which you will inherit all your items. This should have a boolean flag that you set to true when the item is no longer needed.

    Next, add a timer to the view that triggers once every 3 or 5 seconds. When it triggers, look at all the items and delete the ones that are not necessary.

    Another way would be to not use a flag in the item, but instead keep a list of items to be deleted in the view and make the items add themselves to the list when they are no longer needed.

  3. #3
    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: Best way for a graphicsitem to delete itself

    Actually, I think it is better to have the garbage collector in the scene, not the view, because you can have many views for the same items and it could get messy.

  4. #4
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Best way for a graphicsitem to delete itself

    Just to clarify, the code I have does seem to work, (items delete themselves). The only concern I have is if I'm creating a race condition or something. I did have one random crash once and am concerned that it might have been caused by that.

    Garbage collector is an option too, or perhaps emitting a signal to get the scene to delete the item (queuedconnection of course).

  5. #5
    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: Best way for a graphicsitem to delete itself

    Quote Originally Posted by pherthyl View Post
    The only concern I have is if I'm creating a race condition or something. I did have one random crash once and am concerned that it might have been caused by that.
    yes, that is the reason.

    Garbage collector is an option too, or perhaps emitting a signal to get the scene to delete the item (queuedconnection of course).
    I just thought a garbage collector would be more elegant... Queued connections are designed more for inter-thread communications. But I guess they could work... If not, you can always use postEvent.

  6. #6
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Best way for a graphicsitem to delete itself

    Thanks. I'll give the garbage collector a try.

  7. #7
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Best way for a graphicsitem to delete itself

    How about QObject::deleteLater() ...?

  8. #8
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Best way for a graphicsitem to delete itself

    Bingo. Thanks elcuco, I vaguely remembered such a function existing, but couldn't find it anymore.

Similar Threads

  1. Delete dialog
    By Krish_ng in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2007, 13:42
  2. Deleting from TreeWidget... Memoryleak
    By VireX in forum Qt Programming
    Replies: 5
    Last Post: 1st June 2007, 22:33
  3. c++, placement delete upon exception
    By stinos in forum General Programming
    Replies: 6
    Last Post: 31st October 2006, 16:38
  4. Delete all members in a QGraphicsItemGroup
    By vmferreira in forum Qt Programming
    Replies: 3
    Last Post: 17th August 2006, 19:47
  5. QListWidget + Delete Key
    By bpetty in forum Newbie
    Replies: 5
    Last Post: 16th August 2006, 21:38

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.