E
E
Egorithm2017-02-01 16:29:33
Qt
Egorithm, 2017-02-01 16:29:33

Qt. How to create with initialization a class object with member-objects on the stack?

This is how I create member objects in the class:
b33edd8c202245a0867e2a6770c15c7f.png
This is how in the constructor:
0b2cb8b1cbc74ea28350164ca41ba201.png
But how to allocate memory on the stack, and not on the heap?
This is not possible:

QLabel      Title;
QLineEdit   Input;
QLineEdit   Output;
QComboBox   UnitWI;
QComboBox   UnitWO;
QPushButton Button;

You need to initialize, for example like this: Together with functions, like that for static ones, just like:
QLabel Title(this);
QLabel Widget::Title(this);

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Egorithm, 2017-02-01
@EgoRusMarch

I decided. We create objects:

QLabel      Title;
QLineEdit   Input;
QLineEdit   Output;
QComboBox   UnitWI;
QComboBox   UnitWO;
QPushButton Button;

and then set the parent with the setParent() method .
But as it turned out, allocating memory on the stack for widgets is a very bad idea.
This is due to a stack overflow and the fact that when creating several widgets that are not connected by a hierarchy, it will lead to an error ( exception ).

D
devalone, 2017-02-03
@devalone

And you don't create them on the stack ;)

A
abcd0x00, 2017-02-05
@abcd0x00

They then free themselves there (for this you tie them to their parents). Therefore, if you bind to something an element for which no dynamic memory was allocated, then this freer will still release it, which will cause the program to crash. So do as everyone else does - allocate memory and bind.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question