Strange Qt problem

Why does this work:

1
2
   QLabel *a = new QLabel(this);
    a->setText("Test");


but not this:

1
2
       QLabel a (this);
    a.setText("Test");


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.