Results 1 to 8 of 8

Thread: Problem with deleting items from QGraphicsScene

  1. #1
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with deleting items from QGraphicsScene

    Hi,

    I have a problem with deleting QGraphicsItems from a scene. Item (pointer) was created by "new" in widget.cpp so it should be destroyed with "delete" anywhere - in every class. I'm trying to delete item (this) in advance function (located i n paliwo class) with is responsible for animation of my obiect:

    Qt Code:
    1. void paliwo::advance(int phase)
    2. {
    3.  
    4. if(warunek){
    5. QList<QGraphicsItem*>Lista;
    6. samolot *temp_1;
    7. Lista = scene()->items(Qt::AscendingOrder);
    8.  
    9. for(int i = 0;i<Lista.size();++i)
    10. if(Lista.value(i)->zValue() == 4)
    11. temp = Lista.value(i);
    12. temp_1 = dynamic_cast <samolot*>(temp);
    13. temp_1->pobierz(true);
    14. Lista.clear();
    15. Lista.~QList();
    16. warunek = false;
    17. }
    18.  
    19. if (!phase)
    20. return;
    21. else
    22. setPos(this->x(),this->y()+3);
    23. if (pos().y() > 300){
    24. this->~paliwo();
    25. }
    26. }
    27.  
    28.  
    29. paliwo::~paliwo()
    30. {
    31. scene()->removeItem(this);
    32. delete this;
    33. }
    To copy to clipboard, switch view to plain text mode 


    - connected with widget.cpp:
    Qt Code:
    1. timer_piechota = new QTimer(this);
    2. connect(timer_piechota, SIGNAL(timeout()),scena,SLOT(advance()));
    3. connect(timer_piechota, SIGNAL(timeout()),scena,SLOT(update()));
    4. timer_piechota->start(10);
    To copy to clipboard, switch view to plain text mode 



    Object in advance function has parent, so as I guess it shoud be easly removable with "delete". When I'am destroying parent program runs normaly, but after calling destructor of item which I want to destroy program crashes. I don't know what to do
    I don' t want to destroy his parent but for now its only way to destroy it. Please help.
    Last edited by Wojtek_SZ; 19th September 2011 at 13:42.

  2. #2
    Join Date
    Nov 2010
    Posts
    57
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with deleting items from QGraphicsScene

    Calling "delete this;" will call the destructor function ~paliwo()

    Perhaps you need to checkout what delete and a destructor do in C++

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with deleting items from QGraphicsScene

    wow, what are you doing there??? directly calling the destructor??? calling 'delete this'. I think you need to do a bit more research on simple c++ tutorials etc


    For a beginner, never call the destrucor directly. get rid of .~QList and all of instances of calling destructor.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem with deleting items from QGraphicsScene

    I think you just want to remove the item from the scene if a condition is met, so don't call "delete this", simply hide() the item.
    You can then test if its visible when you enter the "advance" method and do the calculations only if its not hidden.
    Some additional comments:
    Qt Code:
    1. if(warunek){
    2. QList<QGraphicsItem*>Lista;
    3. samolot *temp_1;
    4. Lista = scene()->items(Qt::AscendingOrder);
    5.  
    6. for(int i = 0;i<Lista.size();++i)
    7. if(Lista.value(i)->zValue() == 4)
    8. temp = Lista.value(i);
    9. temp_1 = dynamic_cast <samolot*>(temp); // temp_1 can be NULL, in that case...
    10. temp_1->pobierz(true); // you will have a crash here, you need to test the pointer before using it
    11. Lista.clear(); // this is not needed
    12. Lista.~QList(); // this is not needed too, because Lista will go out of scope soon
    13. warunek = false;
    14. }
    To copy to clipboard, switch view to plain text mode 

    What do you mean by calling "delete this" in destructor ?
    Your problems are pure (basic) C++, I think you should begin with a C++ book or two.

  5. #5
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with deleting items from QGraphicsScene

    Thanks for all answers. It seems I need to grab a book and study about memory licking and destructors.

    I made some corrections:

    Qt Code:
    1. void paliwo::advance(int phase)
    2. {
    3.  
    4. if(warunek){
    5. QList<QGraphicsItem*>Lista;
    6. samolot *temp_1;
    7. Lista = scene()->items(Qt::AscendingOrder);
    8.  
    9. for(int i = 0;i<Lista.size();++i)
    10. if(Lista.value(i)->zValue() == 4)
    11. temp = Lista.value(i);
    12. temp_1 = dynamic_cast <samolot*>(temp);
    13. if(temp_1){
    14. temp_1->pobierz(true);
    15. warunek = false;
    16. }
    17. }
    18.  
    19. if (!phase)
    20. return;
    21. else
    22. setPos(mapToParent(0,3));
    23. if (pos().y() > 400)
    24. scene()->removeItem(this);
    25. }
    To copy to clipboard, switch view to plain text mode 

    I decided to use removeItem function. Is it propper in this case?
    According to DOC removes the item and all its children from the scene.
    Removing but not deleting so what's gonna happen with this item after calling this function?
    Allocated memory for item will remain occupied? How does scene handle with items deleting?
    When durring animation item reaches position on scene which is out of QgraphicsView display range, is it automatically deleted?

    stampede wrote:
    temp_1 can be NULL, in that case
    Yes it can. But only in case of incompatibility - in my code class samolot inherits form QGraphicItem, so I think its ok -at least I guess so...
    Once again thanks for all answers

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem with deleting items from QGraphicsScene

    But only in case of incompatibility - in my code class samolot inherits form QGraphicItem, so I think its ok -at least I guess so...
    Without checking for NULL value, you will have a crash soon after you place an item that is not a "samolot" on the scene and give it the zValue = 4.

  7. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with deleting items from QGraphicsScene

    if you remove the item from a scene, you will need to 'delete' it. But this probably means that you need to refactor your code - It is doubtful that an item should be responsible for removing itself from a scene in the first place.

  8. #8
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with deleting items from QGraphicsScene

    I think I get it.

    stampede wrote:
    Without checking for NULL value, you will have a crash soon after you place an item that is not a "samolot" on the scene and give it the zValue = 4.
    Is my code is enough secured in order to avoid such thing (NULL)
    I think I have here clear situation about this practically there is no way to temp_1 be a NULL

    Can anyone answer on my previous questions?

Similar Threads

  1. [pyqt]Tag for items in QGraphicsScene???...
    By supersuraccoon in forum Qt Programming
    Replies: 0
    Last Post: 16th September 2011, 07:28
  2. QGraphicsScene is SLOW with a lot of items !
    By pl01 in forum Qt Programming
    Replies: 11
    Last Post: 5th August 2011, 16:06
  3. How to add items in a QGraphicsScene?
    By schmimona in forum Qt Programming
    Replies: 2
    Last Post: 3rd August 2011, 08:53
  4. Deleting and re-creating a QGraphicsScene
    By dohzer in forum Newbie
    Replies: 7
    Last Post: 18th August 2010, 15:52
  5. How could I save Items on a QGraphicsScene?
    By pinkfrog in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2009, 05:03

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.