PDA

View Full Version : Getting Started Programming with Qt tutorial part2 giving assertion fail



jetimms
29th September 2010, 04:08
I am attempting the tutorial "part2" on http://doc.qt.nokia.com/4.7/gettingstartedqt.html. part1 successfully compiled and ran.

part2, however, is giving me an assertion fail when I exit it.


File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp
Line: 52

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)


This is when I clicked the red X, click the Quit button (part of the tutorial), or Alt-F4.

Here is the tutorial code:


#include <QtGui>

int main(int argv, char **args)
{
QApplication app(argv, args);

QTextEdit textEdit;
QPushButton quitButton("Quit");

QObject::connect(&quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

QVBoxLayout layout;
layout.addWidget(&textEdit);
layout.addWidget(&quitButton);

QWidget window;
window.setLayout(&layout);

window.show();

return app.exec();
}


I've noticed that if the layout.addWidget(... lines are commented out, it does not produce an assertion error.

What could be causing this to happen?
Thanks.

Zlatomir
29th September 2010, 05:23
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)


#include <QtGui>

int main(int argv, char **args)
{
QApplication app(argv, args);

QWidget window; //create the window QWidget first, than the rest
QTextEdit textEdit;
QPushButton quitButton("Quit");
//... rest of the example, obviously don't let two copy of the: QWidget window; line


2) This is the recommended solution: Learn to use widgets on the heap, use pointers, allocate memory with new, and Qt parent-child relationship.


#include <QtGui>

int main(int argv, char **args)
{
QApplication app(argv, args);

QWidget* window = new QWidget; // this is how you allocate on the heap
//the main window/widget can be allocated on the stack


QTextEdit *textEdit = new QTextEdit; //
QPushButton *quitButton = new QPushButton("Quit");

QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit())); //note that you just write the name without & operator

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(textEdit); // -> this is the operator to access members if use pointers
layout->addWidget(quitButton);

window->setLayout(layout); //again without &, and all the widgets added to layout are child of window


window->show();

int r = app.exec(); //this is the proper way to free resources if you allocate the main window/widget om the heap

delete window;//here you free the memory for QWidget window, this will automatically delete the childs of window

return r;
}

jetimms
29th September 2010, 13:31
Interesting!

I haven't seen anything using pointers suggested yet, but I haven't been doing this long. Why the heap instead of the stack, though? Is it to allow the programmer more control of what's using memory?

So, the "parent" object, the window QWidget in this example, needs to be defined and instantiated first?

I'm kind of a completist, so would it be okay to delete everything myself, instead of letting "delete window" propagate them?

Thanks very much for the info!

Zlatomir
29th September 2010, 14:11
I haven't seen anything using pointers suggested yet, but I haven't been doing this long. Why the heap instead of the stack, though? Is it to allow the programmer more control of what's using memory?
Than i really suggest you take a least a C++ tutorial (http://www.cplusplus.com/doc/tutorial/introduction/) before continuing with Qt, you are going to extensivelly use pointers and object oriented programming.

A book is more recommended, you can search for Thinking in C++ written by Bruce Eckel, there are two volumes, free to download.


So, the "parent" object, the window QWidget in this example, needs to be defined and instantiated first?

I'm kind of a completist, so would it be okay to delete everything myself, instead of letting "delete window" propagate them?

The parent child relationship is the Qt way of simplyfing the C++ memory management, so you will need to understand that too, but first "play" a little with pointers and C++ (i really recommend at least the first volume of Thinking in C++), after you are comfortable with basic C++, pointers and OOP, Qt will be a really simple framework.

jetimms
29th September 2010, 14:52
I completed the Koenig & Moo C++ book sometime in 2008, so I am familiar with pointers and iterators on a intermediate-amateur level.

What I meant to say is I haven't seen any Qt references that uses pointers, however I haven't delved too far down into the Qt demo or example code.

I am frankly relieved to see pointers in use. Without them, the beginner Qt code was starting to look a little too much like Visual Basic.

Thanks for your explanation. I can't wait to get back home tonight and try it out!

jetimms
30th September 2010, 13:23
It works!

Actually I tried moving the "QWidget window" declaration and then implemented the program using pointers. Both methods worked, of course.

Just as a note, later in the tutorial it recommended using the heap to allocate.