Results 1 to 6 of 6

Thread: Getting Started Programming with Qt tutorial part2 giving assertion fail

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    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: Getting Started Programming with Qt tutorial part2 giving assertion fail

    You will probably learn to allocate widget on the heap anyway.
    You get the error because of the order in which the stack allocated objects get destructed when they get out of scope.

    So you can do one of the following solutions:
    1)
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argv, char **args)
    4. {
    5. QApplication app(argv, args);
    6.  
    7. QWidget window; //create the window QWidget first, than the rest
    8. QTextEdit textEdit;
    9. QPushButton quitButton("Quit");
    10. //... rest of the example, obviously don't let two copy of the: QWidget window; line
    To copy to clipboard, switch view to plain text mode 

    2) This is the recommended solution: Learn to use widgets on the heap, use pointers, allocate memory with new, and Qt parent-child relationship.
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argv, char **args)
    4. {
    5. QApplication app(argv, args);
    6.  
    7. QWidget* window = new QWidget; // this is how you allocate on the heap
    8. //the main window/widget can be allocated on the stack
    9.  
    10.  
    11. QTextEdit *textEdit = new QTextEdit; //
    12. QPushButton *quitButton = new QPushButton("Quit");
    13.  
    14. QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit())); //note that you just write the name without & operator
    15.  
    16. QVBoxLayout *layout = new QVBoxLayout;
    17. layout->addWidget(textEdit); // -> this is the operator to access members if use pointers
    18. layout->addWidget(quitButton);
    19.  
    20. window->setLayout(layout); //again without &, and all the widgets added to layout are child of window
    21.  
    22.  
    23. window->show();
    24.  
    25. int r = app.exec(); //this is the proper way to free resources if you allocate the main window/widget om the heap
    26.  
    27. delete window;//here you free the memory for QWidget window, this will automatically delete the childs of window
    28.  
    29. return r;
    30. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to Zlatomir for this useful post:

    jetimms (29th September 2010)

Similar Threads

  1. Replies: 5
    Last Post: 19th September 2008, 15:24
  2. Getting started learning network programming
    By as001622 in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2008, 13:02
  3. Debug Assertion Failed
    By ^NyAw^ in forum General Programming
    Replies: 5
    Last Post: 28th December 2007, 11:48
  4. setPixmap cause assertion?
    By Equilibrium in forum Qt Programming
    Replies: 3
    Last Post: 23rd October 2007, 00:34
  5. QGLWIdget renderText() assertion...
    By adonel in forum Qt Programming
    Replies: 3
    Last Post: 22nd October 2007, 21:30

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.