Results 1 to 5 of 5

Thread: Simple RAII question with Qt

  1. #1
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Simple RAII question with Qt

    Hi,

    In the main.cpp I create the main class object. In one of the main class member methods I create a pointer to a data widget, like:
    Qt Code:
    1. void MainClass::showHelpWidget()
    2. {
    3. HelpWidtet* widget = new HelpWidget(this);
    4. widget->show();
    5. }
    6.  
    7. The widget will be deleted when returning from the method and will it be removed from parent's child list?
    To copy to clipboard, switch view to plain text mode 

    The helpWidget contains 3 pointer members which are create in the constructor and deleted in the destructor. Will he destructor be called every time I close the app, or should I set the flag "delete on close" for the help widget?

    Trivial questions, but still looking for a short explanations.


    Thank you.
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Simple RAII question with Qt

    if you set parent (as I see you set it ) then this widget will be deleted when parent will be deleted.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Simple RAII question with Qt

    That is clear. Ok, I misunderstood one thing:

    Qt::WA_DeleteOnClose

    Makes Qt delete this widget when the widget has accepted the close event (see QWidget::closeEvent()).

    Closing a widget is not equal to destroying it, but you can force Qt to destroy a widget on close.

    Let's say I set the flag on my widget. So when is it will be closed it will be deleted and the parent will not to try to delete it again ???
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Simple RAII question with Qt

    when you use this flag then signal QObject::destroyed will be sent immediately before an object will be deleted. so, when you use this flag then by calling close() method, the widget will be deleted after closing and you must to allocate this widget again if you want to display it, but if you don't use this flag then the widget will be deleted when the parent widget will be deleted (of course if you set parent for this widget)
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Oct 2008
    Posts
    70
    Thanks
    1
    Thanked 9 Times in 9 Posts

    Default Re: Simple RAII question with Qt

    Yes, if you will have code like this:

    Qt Code:
    1. void MainClass::showHelpWidget()
    2. {
    3. HelpWidtet* widget = new HelpWidget(this);
    4. widget->setAttribute(Qt::WA_DeleteOnClose);
    5. widget->show();
    6. }
    To copy to clipboard, switch view to plain text mode 

    after closing HelpWidtet it will be destroyed and parent widget (MainClass) will not try to delete it again.

Similar Threads

  1. Simple RegExp Question...
    By jared.carlson23 in forum Newbie
    Replies: 1
    Last Post: 4th July 2008, 14:10
  2. a simple noob question
    By wrproject in forum Newbie
    Replies: 1
    Last Post: 1st July 2008, 23:25
  3. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 00:01
  4. simple thread layout question
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2006, 11:02
  5. QTextEdit Qt4: simple question
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2006, 12: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.