Results 1 to 4 of 4

Thread: Correctly deleting a qwidget with no parent

  1. #1
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Question Correctly deleting a qwidget with no parent

    In my application I have a class that creates a widget with no parent and I want to destroy (call close() and it will automaticaly delete the object by seting the appropiate flag) when it looses the focus. The problem is that if I call the close function in the looseFocus event of the widget, the pointer in my creator class remains valid. I have seen the destroyed signal for QObject that I could use to invalidate the pointer once the widget has been destroyed. As this should be a normal use case I need to know if this is the correct approach to go.

    1) loose focus
    2) call close
    3) wait for the destroyed signal in the creator class
    4) invalidate the pointer in the creator class


    Thanks
    Ramiro

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Correctly deleting a qwidget with no parent

    Calling close does not free the memory you also have to delete it or set the Qt::WA_DeleteOnClose flag. The problem with the pointer can be solved by using QPointer.

    But it seems not right to me. I would emit a signal, when the widget looses its focus and handle all deleting stuff in the "creator class".

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

    boblatino (27th July 2010)

  4. #3
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Correctly deleting a qwidget with no parent

    Quote Originally Posted by Lykurg View Post
    I would emit a signal, when the widget looses its focus and handle all deleting stuff in the "creator class".
    Thanks Lykurg, The only thing Im thinking with that approach is that the code will emit a signal (if its connected as a direct call) it will call close and then delete (but currently the thread is inside the class that is being deleted due to the emit direct call. I have done a test with that and changing the direct to queued mode solved the problem but I want to know if this is the correct way of doing it.


    Thanks
    Ramiro

  5. #4
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Correctly deleting a qwidget with no parent

    Use QObject::deleteLater(), which is practically the same as setting WA_DeleteOnClose, with the difference that the creating class tells the object to delete itself when done, instead of telling it to delete itself in a while.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  6. The following user says thank you to franz for this useful post:

    boblatino (2nd August 2010)

Similar Threads

  1. Promoting the parent QWidget of a QWidget form
    By extrakun in forum Qt Tools
    Replies: 6
    Last Post: 16th April 2010, 15:19
  2. Deleting a QWidget derived object manually
    By hubbobubbo in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2010, 17:32
  3. Replies: 7
    Last Post: 23rd January 2010, 23:59
  4. QWidget *parent = 0 question
    By radek.z in forum Qt Programming
    Replies: 3
    Last Post: 18th May 2009, 10:32
  5. Replies: 1
    Last Post: 28th July 2006, 15:10

Tags for this Thread

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.