Results 1 to 9 of 9

Thread: Could I manage the resource of Qt without try and catch?

  1. #1
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Could I manage the resource of Qt without try and catch?

    Qt Code:
    1. class Wizard : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Wizard(QWidget *parent);
    7.  
    8. private slots:
    9. void what_ever();
    10.  
    11. private:
    12. QPushButton *what_ever;
    13. };
    14.  
    15. Wizard::Wizard( QWidget *parent ) : QDialog(parent)
    16. {
    17. QGridLayout *layout = new QGridLayout( this );
    18.  
    19. QScopedPointer<QTextEdit> textEdit(new QTextEdit);
    20. layout->addWidget( textEdit.take(), 0, 0, 1, 2 );
    21.  
    22. what_ever = new QPushButton("whatever");
    23. layout->addWidget(what_ever, 0, 1, 1, 1);
    24.  
    25. connect(what_ever, SIGNAL(clicked()), this, SLOT(what_ever()));
    26. }
    27.  
    28. void Wizard::what_ever()
    29. {
    30. //blah blah blah
    31. }
    To copy to clipboard, switch view to plain text mode 

    I have some problems about the codes.

    1 : What if textEdit throw exception?
    If "textEdit" throw exception, that means the
    destructor of Wizard would not be called,
    What would Qt handle the resource of "layout"?

    2 : Could I initialize "what_ever" like this?
    layout->addWidget(what_ever = new QPushButton("whatever"), 0, 1, 1, 1);
    Is this safe in Qt4(4.8)?Would it have any change to cause memory leak?

    Thanks a lot
    Last edited by stereoMatching; 8th April 2012 at 09:37.

  2. #2
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Re: Could I manage the resource of Qt without try and catch?

    Qt doesn't really do exceptions. I think that your slot "what_ever" should be able to check whether or not the textEdit is valid.

    I don't think you can initialize the push button like that. For your situation this appears to be the best way:

    Qt Code:
    1. layout->addWidget(new QPushButton("whatever", this), 0, 1, 1, 1);
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. QPushButton *button = new QPushButton("whatever", this);
    2. layout->addWidget(button, 0, 1, 1, 1);
    To copy to clipboard, switch view to plain text mode 

    When you pass QPushButton the "this" pointer, it ensures that the QPushButton will be deleted whenever QDialog is deleted. That's how the parenting system works in Qt... it is quite convenient!

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Could I manage the resource of Qt without try and catch?

    Qt itself uses error codes, so textedit won't throw any exceptions, but you can check this page for exception safety //for std::bad_alloc or other exception that can come from your "environment"

    But i don't understand why do you use QScopedPointer and than take the allocated memory?

  4. #4
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Could I manage the resource of Qt without try and catch?

    Thanks, looks like I only have to handle std::bad_alloc in most of the cases.

    But i don't understand why do you use QScopedPointer and than take the allocated memory?
    My habit when programming without Qt(use objects to manage resources), but in Qt maybe I should just hand it to
    parent rather than smart pointer?

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Could I manage the resource of Qt without try and catch?

    Quote Originally Posted by stereoMatching View Post
    My habit when programming without Qt(use objects to manage resources), but in Qt maybe I should just hand it to
    parent rather than smart pointer?
    The take member function takes the ownership of the allocated memory (so the smart pointer does nothing - you just construct it then take it's allocated memory and make the QScopedPointer null), if it it would do it's job it will delete your QTextEdit at the end of your constructor, so your QTextEdit couldn't be used.

    //i'm not saying that it is a bad thing to use smart pointers, but you need to be careful not to construct the smart pointer when you don't actually use their power.

  6. #6
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Could I manage the resource of Qt without try and catch?

    so the smart pointer does nothing - you just construct it then take it's allocated memory and make the QScopedPointer null
    you are right, I did something meaningless, after all the resource will hand over to the parent.

    I don't think you can initialize the push button like that.
    Could you explain why?Because the error may occur before the button become
    the child of the parent?

    Thanks a lot

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Could I manage the resource of Qt without try and catch?

    2 : Could I initialize "what_ever" like this?
    layout->addWidget(what_ever = new QPushButton("whatever"), 0, 1, 1, 1);
    Is this safe in Qt4(4.8)?Would it have any change to cause memory leak?
    I don't see why not, but I also don't see what you gain by doing so. If the new call fails with bad_alloc then you are in the same situation: an unhandled exception will terminate the program.

    Layouts do not own their content. They do ensure that their content is parented to the widget they are applied to... if there is one. With your code: if layout has been applied to a widget then adding the push button to the layout will give it a parent and the memory is owned by the widget. If the layout has not yet been applied to a widget then the QPushButton does not have a parent QWidget and the standard Qt ownership mechanism cannot clean up the push button. So, depending on circumstances you could leak the QPushButton memory between this call and when the layout is applied to a widget.

    The idiomatic:
    Qt Code:
    1. what_ever = new QPushButton("whatever", this); // widget is owned by "this" (usually another widget)
    2. layout->addWidget(what_ever, 0, 1, 1, 1);
    To copy to clipboard, switch view to plain text mode 
    ensures that the new widget is at all times owned regardless of the state of the layout.

  8. #8
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Could I manage the resource of Qt without try and catch?

    I don't see why not, but I also don't see what you gain by doing so
    Because I want to make the codes become shorter
    personal coding style, I like that kind of codes better
    I think this is acceptable?At least this is not as complicated as
    Qt Code:
    1. std::v.erase(std::remove_if(std::find_if(std::begin(v), std::end(v), std::bind//blah blah blah) ) );
    To copy to clipboard, switch view to plain text mode 

    Layouts do not own their content. They do ensure that their content is parented to the widget they are applied to... if there is one
    That means if I already assign a parent to the layout like this
    Qt Code:
    1. QGridLayout *layout = new QGridLayout( this );
    2. layout->addWidget(mbutton = new QPushButton("mm"), 0, 0);
    To copy to clipboard, switch view to plain text mode 
    it would be save even I don't declare the parent of QPushButton because the layout
    would make "this" as the parent of the mButton?

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

    Default Re: Could I manage the resource of Qt without try and catch?

    Quote Originally Posted by stereoMatching View Post
    Because I want to make the codes become shorter
    personal coding style, I like that kind of codes better
    You can make your code even shorter by removing all newline characters and you can also make all your identifiers single or double letter only.
    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.


Similar Threads

  1. best way to manage forms?
    By skuda in forum Qt Programming
    Replies: 4
    Last Post: 7th December 2009, 08:38
  2. Use Organigramm to manage database
    By Mefisto in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2009, 10:45
  3. What is the best way to manage memory across threads?
    By bughunter2 in forum Qt Programming
    Replies: 2
    Last Post: 4th January 2009, 23:53
  4. Best way to manage application icons
    By SiLiZiUMM in forum Qt Programming
    Replies: 0
    Last Post: 23rd April 2008, 14:28
  5. How to manage QPainter?
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 28th April 2006, 13:20

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.