Getting Started Programming with Qt tutorial part2 giving assertion fail
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.
Code:
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:
Code:
#include <QtGui>
int main(int argv, char **args)
{
QObject::connect(&quitButton,
SIGNAL(clicked
()),
qApp,
SLOT(quit
()));
layout.addWidget(&textEdit);
layout.addWidget(&quitButton);
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.
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)
Code:
#include <QtGui>
int main(int argv, char **args)
{
QWidget window;
//create the window QWidget first, than the rest //... 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.
Code:
#include <QtGui>
int main(int argv, char **args)
{
QWidget* window
= new QWidget;
// this is how you allocate on the heap //the main window/widget can be allocated on the stack
QObject::connect(quitButton,
SIGNAL(clicked
()),
qApp,
SLOT(quit
()));
//note that you just write the name without & operator
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;
}
Re: Getting Started Programming with Qt tutorial part2 giving assertion fail
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!
Re: Getting Started Programming with Qt tutorial part2 giving assertion fail
Quote:
Originally Posted by
jetimms
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 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.
Quote:
Originally Posted by
jetimms
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.
Re: Getting Started Programming with Qt tutorial part2 giving assertion fail
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!
Re: Getting Started Programming with Qt tutorial part2 giving assertion fail
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.