Strange Qt problem

Why does this work:

   QLabel *a = new QLabel(this);

but not this:

       QLabel a (this);

Aren't these pretty much identical statements (one is static, the other dynamic). By work I mean display as it should do, both compile.
I feel like I'm missing something stupid.
Last edited on
Because scope and lifetime are related.

The first creates a QLabel that has a global lifetime -- the scope of the local pointer variable does not affect the lifetime of the global (dynamic) object (label).

The second creates a QLabel that is local to the current scope -- linked to the local variable that names it. When the local variable goes out of scope (that is, when the function that creates the label terminates), the QLabel is also destroyed. This happens so fast that you never see anything appear on screen.

Hope this helps.
Are you calling the show() function for both pieces of code?
Topic archived. No new replies allowed.