Results 1 to 14 of 14

Thread: Delete QWidget

  1. #1
    Join Date
    Jan 2012
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Delete QWidget

    Hi,

    I'm a little bit confused about the safe deletion of a QWidget. If I create a QWidget with

    Qt Code:
    1. MyWidget* myWidget = new MyWidget();
    To copy to clipboard, switch view to plain text mode 

    what is the correct way to delete the Widget?

    Qt Code:
    1. delete myWidget;
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. myWidget.close();
    2. delete myWidget;
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. myWidget.close();
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Delete QWidget

    Qt Code:
    1. myWidget->deleteLater();
    To copy to clipboard, switch view to plain text mode 

    close() doesn't delete anything, it just hides the widget.
    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.


  3. #3
    Join Date
    Sep 2011
    Posts
    86
    Qt products
    Qt4 Qt5
    Platforms
    Windows
    Thanks
    4
    Thanked 4 Times in 4 Posts

    Default Re: Delete QWidget

    function close() only return information whether your widget is closed (hidden) or not. It has nothing to do with memory release.

    When you're creating QWidgets on a stack, for instance like that:

    Qt Code:
    1. {
    2. QWidget widget();
    3. }
    To copy to clipboard, switch view to plain text mode 

    When it'll go out of scope {} widget will be delete automatically.

    When you create QWidget on heap there are two ways

    I. You give a pointer to parent as an argument when create widget and than you don't have to think about deleting it later:

    Qt Code:
    1. QWidget *myWidget = new QWidget(parent)
    To copy to clipboard, switch view to plain text mode 

    usually parent = this, when you create widgets in constructor of your mainWindow widget

    II. When you don't provide pointer to parent widget you have to delete it by yourself, you can do it either in destructor of one level up widget or
    after closing your widget like that:

    Qt Code:
    1. QWidget *myWidget = new QWidget();
    2.  
    3. myWidget->show();
    4.  
    5. if(myWidget->close)
    6. {
    7. delete myWidget;
    8. }
    To copy to clipboard, switch view to plain text mode 

    But the second way looks strange and I don't know where it could be useful in typical applications.

    I do it like that:
    1. Derive my mainWindow from QWidget
    2. In constructor create QWidgets on heap with parent pointer as arguments of QWidget constructors
    3. Create myWindow widget in main.cpp on stack: MyWindow window; Then window.show();
    4. Execute event loop and program after closing main window delete everything on its own.

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

    Default Re: Delete QWidget

    @code_err: Are you sure that what you have written is correct?

    For instance, what is this code going to do?

    Qt Code:
    1. QWidget *myWidget = new QWidget();
    2.  
    3. myWidget->show();
    4.  
    5. if(myWidget->close)
    6. {
    7. delete myWidget;
    8. }
    To copy to clipboard, switch view to plain text mode 
    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.


  5. #5
    Join Date
    Sep 2011
    Posts
    86
    Qt products
    Qt4 Qt5
    Platforms
    Windows
    Thanks
    4
    Thanked 4 Times in 4 Posts

    Default Re: Delete QWidget

    Yeah, i know, this code does nothing and is not correct. Widget will never be deleted. If i want it to be deleted when widget gets hide i would probably do something with eventFilter but it is like shooting to fly with shotgun i think.

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

    Default Re: Delete QWidget

    I would say this code wouldn't even compile since close() is a method.

    If one wants to delete a widget when it's closed, one uses Qt::WA_DeleteOnClose attribute.
    Last edited by wysota; 29th February 2012 at 16:34.
    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.


  7. #7
    Join Date
    Sep 2011
    Posts
    86
    Qt products
    Qt4 Qt5
    Platforms
    Windows
    Thanks
    4
    Thanked 4 Times in 4 Posts

    Default Re: Delete QWidget

    since close() is a method and it is returning void.
    Close is a method aka slot but it returns bool value not void.

    Sorry, my fault. I do it all the time.
    It should be of course close() but...
    Closes this widget. Returns true if the widget was closed; otherwise returns false.
    I didn't see first line and thought that close() returns information whether QWidget was closed.

  8. #8
    Join Date
    Jan 2012
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Delete QWidget

    Quote Originally Posted by wysota View Post
    Qt Code:
    1. myWidget->deleteLater();
    To copy to clipboard, switch view to plain text mode 
    If I call deleteLater(), can I be sure that the Widget gets closed and that I can reuse the myWidget Pointer to create a new Widget immediately afterwards?


    Qt Code:
    1. myWidget->deleteLater();
    2. myWidget = new MyWidget();
    To copy to clipboard, switch view to plain text mode 

    What is the difference between

    Qt Code:
    1. delete myWidget;
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. myWidget->deleteLater();
    To copy to clipboard, switch view to plain text mode 

    ?

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

    Default Re: Delete QWidget

    Quote Originally Posted by StarShaper View Post
    If I call deleteLater(), can I be sure that the Widget gets closed and that I can reuse the myWidget Pointer to create a new Widget immediately afterwards?
    You can reuse the pointer any time you want, even without deleting the previous object assigned to it. When a widget is deleted, it gets removed from the screen if it's there.


    Qt Code:
    1. myWidget->deleteLater();
    2. myWidget = new MyWidget();
    To copy to clipboard, switch view to plain text mode 

    What is the difference between

    Qt Code:
    1. delete myWidget;
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. myWidget->deleteLater();
    To copy to clipboard, switch view to plain text mode 

    ?
    The difference is described in the docs -- QObject::deleteLater()
    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.


  10. #10
    Join Date
    Jan 2012
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Delete QWidget

    Quote Originally Posted by wysota View Post
    You can reuse the pointer any time you want, even without deleting the previous object assigned to it.
    And what happens to the object, if I reuse the pointer, without deleting the QWidget before? Is this some kind of auto_ptr, where the destructor is called automatically, when no pointer points to the object?

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

    Default Re: Delete QWidget

    Quote Originally Posted by StarShaper View Post
    And what happens to the object, if I reuse the pointer, without deleting the QWidget before?
    Nothing. It just sits somewhere in the memory. It's just the pointer is not tied to the object in any definite way.

    Is this some kind of auto_ptr, where the destructor is called automatically, when no pointer points to the object?
    No.
    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.


  12. #12
    Join Date
    Jan 2012
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Delete QWidget

    Quote Originally Posted by wysota View Post
    Nothing. It just sits somewhere in the memory. It's just the pointer is not tied to the object in any definite way.
    Ok, that's actually my question. If I don't delete the object myself, than I have a memory leak, right?

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

    Default Re: Delete QWidget

    Quote Originally Posted by StarShaper View Post
    If I don't delete the object myself, than I have a memory leak, right?
    It depends. If the object has a parent or is owned by some other object, that object will take care of deleting it.
    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.


  14. #14
    Join Date
    Jan 2012
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Delete QWidget

    Quote Originally Posted by wysota View Post
    It depends. If the object has a parent or is owned by some other object, that object will take care of deleting it.
    Ok, that was the point. Thanks!

Similar Threads

  1. Replies: 3
    Last Post: 1st April 2010, 23:56
  2. delete QWidget
    By yxtx1984 in forum Newbie
    Replies: 1
    Last Post: 10th March 2010, 04:17
  3. "new" + "delete" a QWidget and its children
    By vkincaid in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2010, 21:51
  4. Replies: 4
    Last Post: 19th February 2009, 11:10
  5. QWidget delete at once
    By bunjee in forum Qt Programming
    Replies: 3
    Last Post: 14th February 2008, 13:46

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.