Pointer usage in Qt
-
Hi folks,
I have just started the Qt GUI programming and in the first lecture
of my book use very much pointers that are not suggested in C++.
Why people use that's kind of code;
@#include <QtGui/QApplication>
#include <QtGui/QHBoxLayout>
#include <QtGui/QSlider>
#include <QtGui/QSpinBox>int main(int argc, char *argv[])
{
QApplication app(argc, argv);QWidget *window = new QWidget;
window->setWindowTitle("Enter Your Age");QSpinBox *spinBox = new QSpinBox;
QSlider *slider = new QSlider(Qt::Horizontal);
spinBox->setRange(0, 130);
slider->setRange(0, 130);QObject::connect(spinBox, SIGNAL(valueChanged(int)),
slider, SLOT(setValue(int)));QObject::connect(slider, SIGNAL(valueChanged(int)),
spinBox, SLOT(setValue(int)));spinBox->setValue(35);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(spinBox);
layout->addWidget(slider);window->setLayout(layout);
window->show();return app.exec();
}@Why not they don't use that kind
@#include <QtGui/QApplication>
#include <QtGui/QWidget>
#include <QtGui/QSlider>
#include <QtGui/QSpinBox>
#include <QtGui/QHBoxLayout>int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("Enter Your Age");QSpinBox spinBox; QSlider slider(Qt::Horizontal); spinBox.setRange(0, 130); slider.setRange(0, 130); QObject::connect(&spinBox, SIGNAL(valueChanged(int)), &slider, SLOT(setValue(int))); QObject::connect(&slider, SIGNAL(valueChanged(int)), &spinBox, SLOT(setValue(int))); spinBox.setValue(35); QHBoxLayout layout; layout.addWidget(&spinBox); layout.addWidget(&slider); window.setLayout(&layout); window.show(); return app.exec();
}@
-
Well... it is definitely a long story and everything depends on the purpose :)
Try to find and read articles about the memory management in Qt. May be the documentation of "QObject":http://doc.qt.nokia.com/latest/qobject.html is a good start. The pattern for using parents and childs is very important for Qt. Because the destructor of a parent object destroys all child objects and this feature facilitate the use of pointers. Please also note Qt provide "smart pointers":http://labs.qt.nokia.com/2009/08/25/count-with-me-how-many-smart-pointer-classes-does-qt-have/
-
I would also suggest studying http://qt.nokia.com/partners/qt-in-education/qt-in-education-course-material/ [qt.nokia.com]
-
Qt object model takes care of object destruction, ie. the parent deletes all of its children. When objects are created on the heap you do not have to think about the order of constructing them. When you use a stack the order of construction/destruction is fixed therefore you must create all objects in correct order otherwise your application will crash.
This code will not crash:
@QApplication app(argc, argv);QWidget w; QLabel label("label"); label.setParent(&w); w.show(); return app.exec();
@
But this code will crash:
@ QApplication app(argc, argv);QLabel label("label"); QWidget w; label.setParent(&w); w.show(); return app.exec();
@
Because of that there is a good rule that nearly all objects which are derived from QObject class should be created on the heap.
Who told you that pointers are not suggested in C++? It is not exactly as you have been told, sometimes you have to or should allocate on the heap, like for example a big class. Big (in terms of sizeof) classes should be allocated on the heap, because stack is a limited resource where the heap is a little bit less limited resource :)
Cheers
-
[quote author="bergo.torino" date="1308410250"]Who told you that pointers are not suggested in C++? It is not exactly as you have been told, sometimes you have to or should allocate on the heap, like for example a big class. Big (in terms of sizeof) classes should be allocated on the heap, because stack is a limited resource where the heap is a little bit less limited resource :)[/quote]
Not only size matters, also lifetime. Sometimes you have to create objects inside classes, but not in the constructor (perhaps you don't know there, which object to create) or need plug-in mechanisms, to create objects on the fly, ...
All this needs newing objects on the heap.
The only thing with newed objects is, take care of deletion :-) That's why I prefer to use smart pointers etc.
-
[quote author="bergo.torino" date="1308410250"]Qt object model takes care of object destruction, ie. the parent deletes all of its children. When objects are created on the heap you do not have to think about the order of constructing them. When you use a stack the order of construction/destruction is fixed therefore you must create all objects in correct order otherwise your application will crash.
This code will not crash:
@QApplication app(argc, argv);QWidget w; QLabel label("label"); label.setParent(&w); w.show(); return app.exec();
@
But this code will crash:
@ QApplication app(argc, argv);QLabel label("label"); QWidget w; label.setParent(&w); w.show(); return app.exec();
@
Because of that there is a good rule that nearly all objects which are derived from QObject class should be created on the heap.[/quote]
Children are deleted by the parent really acceptable but who deletes the parent?
What if I take care of the order of classes?[quote author="bergo.torino" date="1308410250"]
Who told you that pointers are not suggested in C++? It is not exactly as you have been told, sometimes you have to or should allocate on the heap, like for example a big class. Big (in terms of sizeof) classes should be allocated on the heap, because stack is a limited resource where the heap is a little bit less limited resource :)Cheers[/quote]
Many C++ books mention about it. If you want to use heap which we call it free store in C++, you should use smart pointers like auto_ptr or other ones implemented by boost or Qt's ones which is told by @leon.anavi. That's a kind of pointer usage is not acceptable in standart C++ because of exception handing mechanism. -
[quote author="quasimodo" date="1308432868"]Children are deleted by the parent really acceptable but who deletes the parent?
What if I take care of the order of classes?[/quote]It's irrelevant who deletes the parent, as long as someone deletes the parent. The parent is usually the one object that can be created on the stack, simply because there is no object deleting it.[quote author="quasimodo" date="1308432868"]If you want to use heap which we call it free store in C++, you should use smart pointers like auto_ptr or other ones implemented by boost or Qt's ones which is told by @leon.anavi. That's a kind of pointer usage is not acceptable in standart C++ because of exception handing mechanism.
[/quote]C++ is a free form, "to each their own" type of language. The fact that references and templates have been added to allow you to write safer, more readable code doesn't mean that you have to use them. Nor does it mean that they will actually help you to write safer and more readable code. Books can teach you a lot, but in my experience, practice always comes with cases that books don't really handle.
Writing exception safe code is a task for the programmer. In most cases, developers will pass on the C++ exception handling, simply because it's considered tricky at best. Ah well, let's not get into that. For me personally, using smart pointers without exception for all heap allocated objects in not acceptable. It's often creating more code bloat than it solves, but again, to each their own.