[Interest] Create the main widget on heap in main()

Etienne Sandré-Chardonnal etienne.sandre at m4x.org
Fri Jan 10 14:17:45 CET 2014


I don't see anything wrong in this example?

1) QApplication is created
2) QLabel is created and shown
3) The event loop starts and exec will return when app.quit() is called,
which is done automatically when the QLabel is closed (because it has no
parent, so it has Qt::Window flag, so it has Qt::WA_QuitOnClose flag by
default)

You just forgot to delete label. However it CANNOT live longer than app, as
it runs in its event loop.

Etienne


2014/1/10 程梁 <devbean at outlook.com>

> Hi, there! I wonder if I could create the main widget on heap in main()?
> Following is my example code:
>
> int main(int argc, char *argv[])
> {
>     QApplication app(argc, argv);
>     QLabel *label = new QLabel;
>     label->show();
>     return app.exec();
> }
>
> label should live longer than app so there might be unsafe because any
> QPaintDevice must have a QApplication.
> I'm not sure am I right? That's all main widgets should create on stack in
> main() in order to avoid such things? Or there is no such requirement? If
> it should on stack, could you give me some examples about the crash when
> created on heap?
>
> Thank you!
>
> Cheng Liang
> Nanjing, China
> http://www.devbean.net
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140110/6b587685/attachment.html>


More information about the Interest mailing list