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

Alejandro Exojo suy at badopi.org
Sat Jan 11 11:01:54 CET 2014


El Saturday 11 January 2014, 程梁 escribió:
> Yes, I tought there is no any problem about this sample demo. But is there
> any posibility when the application is large? According to your words I
> don't see any problem about creating the main widget on heap because the
> main event loop will close all visible widgets. And no widget should be
> created in other thead so all widgets must be in main thread, that means
> they will be closed at last. So it is OK when you create the main widget
> on heap in main, am I right?

What I don't understand yet is the reason to do it, though. Why not create 
them as local variables?

If you insist in doing it that way, is OK, just make sure you don't leak any 
memory/resources. One pattern that I saw for creating applications on Sailfish 
OS is the following (and that I don't say is either good or bad):

    QScopedPointer<QApplication> app(new QApplication(argc, argv));
    QScopedPointer<QQuickView> view(new QQuickView);
    view->setSource("/path/to/main.qml");
(...)
    return app->exec();

So, basically is the same, but the objects are instantiated with new, and you 
make sure you don't leak them through a smart pointer class.

For your example with a QLabel is easier, since if the object has a parent, 
you can forget about it, and the parent will delete all its children.

-- 
Alex (a.k.a. suy) | GPG ID 0x0B8B0BC2
http://barnacity.net/ | http://disperso.net



More information about the Interest mailing list