[SOLVED] Adding widgets to a layout inside it's own constructor
I have three classes: the first is a QLabel and the second is a QLineEdit. The plan is to have these be the 'Read' and 'Edit' modes of a 'Note', with these mode being switched on and off at the whim of the user. I plan to use my third class, which is derived from QStackedWidget, to enable the switching.
My problem is with adding the two text widgets to the stacked widget. The documentation say that you add widgets to a QStackedWidget the same way you would add them to any other layout, and I was hoping to do this in the constructor of the stacked widget class, but I'm unsure how the program will behave if I try to add objects to a layout class while it's still being constructed.
To make things a bit more clear, here's some example code:
@class Note : public QStackedWidget
I know in general it's not safe to use the 'this' pointer in the constructor since the object isn't fully formed yet, but I'm not really sure what's going on when QWidgets are added to layouts so I can't be say for sure.
Also, I'm new to Qt and GUI design in general, so if I'm going about implementing this sort of functionality in completely the wrong way, I'm open to suggestions for improving it.
[quote]I know in general it’s not safe to use the ‘this’ pointer in the constructor since the object isn’t fully formed yet[/quote]
This is wrong. http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.7
The pattern you show there is widely used in Qt, and in general safe to use.
In general the use of 'own/this' function in the constructor is only 'not sure' in C++ only if you are using virtual.
The class is always fully constructed regarding itself and parents (inheritenance) but not from of the point of view of a possible child which inherits of you, because this one is not created.
Mainly you should never call a virtual from this constructor or at least make sur that you are on you level: this->Note::addWidget(...);
Now for the philosphy part: constructing widget in the constructor of a widget is not really a problem, it's the role of the constructor to 'construct' object and to link them.