June 18, 2011

quasimodo quasimodo
Lab Rat
2 posts

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;

  1. #include <QtGui/QApplication>
  2. #include <QtGui/QHBoxLayout>
  3. #include <QtGui/QSlider>
  4. #include <QtGui/QSpinBox>
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.  QApplication app(argc, argv);
  9.  
  10.  QWidget *window = new QWidget;
  11.  window->setWindowTitle("Enter Your Age");
  12.  
  13.  QSpinBox *spinBox = new QSpinBox;
  14.  QSlider *slider = new QSlider(Qt::Horizontal);
  15.  spinBox->setRange(0, 130);
  16.  slider->setRange(0, 130);
  17.  
  18.  QObject::connect(spinBox, SIGNAL(valueChanged(int)),
  19.      slider, SLOT(setValue(int)));
  20.  
  21.  QObject::connect(slider, SIGNAL(valueChanged(int)),
  22.      spinBox, SLOT(setValue(int)));
  23.  
  24.  spinBox->setValue(35);
  25.  
  26.  QHBoxLayout *layout = new QHBoxLayout;
  27.  layout->addWidget(spinBox);
  28.  layout->addWidget(slider);
  29.  
  30.  window->setLayout(layout);
  31.  window->show();
  32.  
  33.  return app.exec();
  34.  }

Why not they don’t use that kind

  1. #include <QtGui/QApplication>
  2. #include <QtGui/QWidget>
  3. #include <QtGui/QSlider>
  4. #include <QtGui/QSpinBox>
  5. #include <QtGui/QHBoxLayout>
  6.  
  7.  
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11.     QApplication app(argc, argv);
  12.     QWidget window;
  13.     window.setWindowTitle("Enter Your Age");
  14.  
  15.     QSpinBox spinBox;
  16.     QSlider slider(Qt::Horizontal);
  17.     spinBox.setRange(0, 130);
  18.     slider.setRange(0, 130);
  19.  
  20.     QObject::connect(&spinBox, SIGNAL(valueChanged(int)),
  21.                      &slider, SLOT(setValue(int)));
  22.  
  23.     QObject::connect(&slider, SIGNAL(valueChanged(int)),
  24.                      &spinBox, SLOT(setValue(int)));
  25.  
  26.     spinBox.setValue(35);
  27.  
  28.     QHBoxLayout layout;
  29.     layout.addWidget(&spinBox);
  30.     layout.addWidget(&slider);
  31.  
  32.     window.setLayout(&layout);
  33.     window.show();
  34.  
  35.     return app.exec();
  36. }

 Signature 

Ali

6 replies

June 18, 2011

leon.anavi leon.anavi
Area 51 Engineer
1217 posts

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 [doc.qt.nokia.com] 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 [labs.qt.nokia.com]

 Signature 

http://anavi.org/

June 18, 2011

Stavros Filippidis Stavros Filippidis
Lab Rat
388 posts

I would also suggest studying http://qt.nokia.com/partners/qt-in-education/qt-in-education-course-material/ [qt.nokia.com]

June 18, 2011

bergo.torino bergo.torino
Lab Rat
41 posts

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:

  1. QApplication app(argc, argv);
  2.  
  3.     QWidget w;
  4.     QLabel label("label");
  5.     label.setParent(&w);
  6.     w.show();
  7.  
  8.     return app.exec();

But this code will crash:

  1.  QApplication app(argc, argv);
  2.  
  3.     QLabel label("label");
  4.     QWidget w;
  5.     label.setParent(&w);
  6.     w.show();
  7.  
  8.     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

 Signature 

It’s compiling - mostly I’m slacking off ;)

June 18, 2011

Gerolf Gerolf
Robot Herder
3287 posts
bergo.torino wrote:
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 :)

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.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

June 18, 2011

quasimodo quasimodo
Lab Rat
2 posts

bergo.torino wrote:
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:

  1. QApplication app(argc, argv);
  2.  
  3.     QWidget w;
  4.     QLabel label("label");
  5.     label.setParent(&w);
  6.     w.show();
  7.  
  8.     return app.exec();

But this code will crash:

  1.  QApplication app(argc, argv);
  2.  
  3.     QLabel label("label");
  4.     QWidget w;
  5.     label.setParent(&w);
  6.     w.show();
  7.  
  8.     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.

Children are deleted by the parent really acceptable but who deletes the parent?
What if I take care of the order of classes?

bergo.torino wrote:

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


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.

 Signature 

Ali

June 18, 2011

Franzk Franzk
Lab Rat
840 posts

quasimodo wrote:
Children are deleted by the parent really acceptable but who deletes the parent?
What if I take care of the order of classes?
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.

quasimodo wrote:
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.

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.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

 
  ‹‹ Show UI Design not in main metod is possible?      C++ global class varible definition ››

You must log in to post a reply. Not a member yet? Register here!