Qt and pointers

I am new to Qt and I read a few Qt projects to get the basics. When browsing through various classes, it seems that pointers to QWidget are always used.

Example:

class ExempleWidget : public QWidget { Q_OBJECT public: ExempleWidget(); ~ExempleWidget(); private: QPushButton * m_stopButton; QPushButton * m_playButton; QHBoxLayout * m_hBoxLayout; QVBoxLayout * m_vBoxLayout; }; 

Is there any reason to use pointers for "QPushButton" * "QHBoxLayout" etc.? Can we just use values, so we don’t need to worry about “new” and “delete”? Well, I know we can, but is this a good way to do this?

eg.

 private: QPushButton m_stopButton; 

EDIT:

All answers seem to point to the hierarchy between the Qt classes. This is a good enough reason for me, and I have no doubt about it, but I will say that there is no connection between the widgets of my class (* m_stopButton * and * m_playButton *).

I can safely declare them as values ​​in this case, right? When the class is deleted, the members will be deleted so that pointers or not, we will have the same behavior.

+8
c ++ pointers qt
source share
5 answers

Since in the constructor you can provide the parents, so when the parent is removed, they are automatically deleted.

Depending on the project, there may be other reasons (for example, the creation and destruction of methods depending on the logic of the class).

+3
source share

You want to use pointers if you want all control over the lifetime of an object. for example

  • If you want to build a class member at some point after processing the initialization list in the constructor.

  • If you want to destroy a class member at some point before starting the epilogue destructor created by the compiler.

It is completely safe not to use pointers when you are fine with creating a compiler and destroying object instances for you.

Raw pointers are prone to errors whenever they own the resource they point to: it is an error to forget to delete the resource that belongs to it, delete the resource several times, or dereference an already deleted resource.

The use of raw pointers (as opposed to smart pointers) that do not own the resource — when the resource is freed by someone else — can still cause premature optimization. Using source pointers is prone to turning “small” code changes into resource leaks:

 class X : public QObject { QObject * a; public: // OK X() : a(new QObject(this)) {} // an error - we need to add a destructor that deletes a X() : a(new QObject) {} } class X : public QObject P QScopedPointer<QObject> a; public: // Both OK X() : a(new QObject(this)) {} X() : a(new QObject) {} }; 

Smart pointers - C ++ 11 std::unique_ptr , Qt 4.6 QScopedPointer - ensure that deletion always occurs, and that access to the remote resource is equivalent to dereferencing the null pointer and, thus, is nominally caught and signaled by the processor equipment.

In C ++, the order of constructing and destroying class members and automatic variables is fixed. In the examples below, the construction order is always a , then b , and the destruction order is always b , then a . This is very important .

 class C { A a; B b; }; void test() { A a; B b; } 

Thus, the following code is valid and behaves correctly, at least in Qt 3, Qt 4 and Qt 5, unless otherwise specified.

 class C : public QWidget { QWidget a; QLabel b; public: C(QWidget * parent = 0) : QWidget(parent), a(this), b(&a) {} } // Qt 4.6 & up only class C : public QWidget { QScopedPointer<QWidget> a; QScopedPointer<QLabel> b; public: C(QWidget * parent = 0) : QWidget(parent), a(new QWidget(this)), b(new QLabel(a.data())) {} }; // C++11 only class C : public QWidget { std::unique_ptr<QWidget> a; std::unique_ptr<QLabel> b; public: C(QWidget * parent = 0) : QWidget(parent), a(new QWidget(this)), b(new QLabel(a.data())) {} }; void test() { QWidget c; QWidget a(&c); QLabel b(&a); } 

In all cases, the order of construction: c, a, b ; destruction order: b, a, c .

+5
source share

Qt is mainly designed as C ++ anno 1996 or so (or very Java-esque, if you like). Lots of pointers, lots of inheritance.

Of course, one of the reasons they often use pointers is that inheritance is almost universal, and it avoids the problems associated with cuts.

Another reason is that Qt has a strong agreement that memory management is handled by providing each parent object, so that when the parent object is destroyed, it calls delete its children.

If you declare your object as a local object instead of a pointer to something highlighted with new , then you must be careful not to give it a parent, or otherwise ensure that it goes out of scope to its parent so that delete never called .

+4
source share

Yes, there is a reason. All subclasses of QObject can be placed in a hierarchy using the parent attribute. This is very useful because it allows you to allow a QObject action for the entire hierarchy of children. One of these actions is show or hide for widgets, the other is to destroy the object.

When you delete a QObject (i.e. a Qwidget ), all of its children are also deleted. as doc says

Warning: all child objects are deleted. If any of these objects has a stack enabled or global, sooner or later your program will crash.

In addition, you can move objects to other threads. To do this, you need this object to be in the memory area accessible to all threads in the process. Stack for one thread, so it is not suitable.

In terms of pure language , you only have a constructor

 QObject::QObject( QObject * parent = 0 ); 

If you provide a value to a parent other than NULL , that value must have a longer lifetime than the object that is being created. Otherwise, it may use an invalid link.

Basically, they had different mechanisms that they wanted to implement, and with various restrictions (including backward compatibility), they chose.

+2
source share

The reason is historical. In older versions of Qt, when the parent widget is passed to the widget constructors, the parent QWidget::~QWidget calls delete child widgets.

A check has been added in current versions of Qt to see if the child widget has already been removed, which means that now you can make them regular members.

-one
source share

All Articles