Results 1 to 3 of 3

Thread: Question about the undo framework example

  1. #1
    Join Date
    Jul 2016
    Posts
    19
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Question about the undo framework example

    Hello,

    My question is about the undoframework http://doc.qt.io/qt-5/qtwidgets-tool...k-example.html and http://doc.qt.io/qt-5/qtwidgets-tool...mands-cpp.html

    The AddCommand class has a destructor :

    Qt Code:
    1. AddCommand::~AddCommand()
    2. {
    3. if (!myDiagramItem->scene())
    4. delete myDiagramItem;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Let's say that the stored item, myDiagramItem, is inside a scene. The scene has the responsability to delete it. So I wonder how this destructor can work because if the destructor of the scene is called before the destructor of AddCommand the pointer myDiagramItem will be invalid, so the first line (if (!myDiagramItem->scene()) ) should lead to a crash. How can we be sure that it will not happened ?


    Second question, it seems that diagramScene stored in MainWindow doesn't have any parent, so how the memory of diagramScene is released ?
    (See : http://doc.qt.io/qt-5/qtwidgets-tool...indow-cpp.html)

    Thank you very much for your help.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Question about the undo framework example

    Quote Originally Posted by nilot View Post
    How can we be sure that it will not happened ?
    One of your options is to track the undo/redo state. I.e. if your command knows that it is in state after undo then it owns the pointer, if it is in the state after redo, then the scene owns the pointer.

    Or you pass a pointer/reference of the command to the item and the item "notifies" the command when it is being deleted.

    Quote Originally Posted by nilot View Post
    Second question, it seems that diagramScene stored in MainWindow doesn't have any parent, so how the memory of diagramScene is released ?
    If you don't pass a parent, you need are in basic C++ land: you delete it yourself or store the raw pointer in a smart pointer that deletes its data on destruction.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    nilot (10th February 2017)

  4. #3
    Join Date
    Jul 2016
    Posts
    19
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Question about the undo framework example

    Thank you, I agree with everything you said, I am just wondering why this isn't done in the example.

Similar Threads

  1. undo question with removeItem
    By mooreaa in forum Qt Programming
    Replies: 9
    Last Post: 22nd December 2014, 22:42
  2. Replies: 1
    Last Post: 20th March 2014, 17:21
  3. Replies: 0
    Last Post: 21st January 2014, 06:05
  4. Qt Property Browser Framework - Redo/Undo
    By monadic_kid in forum Qt Programming
    Replies: 2
    Last Post: 3rd December 2009, 10:46
  5. MAC install question -no-framework
    By patrik08 in forum Installation and Deployment
    Replies: 0
    Last Post: 11th June 2007, 12:31

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.