[Interest] Create the main widget on heap in main()
Scott Aron Bloom
scott.bloom at onshorecs.com
Sat Jan 11 12:33:54 CET 2014
In general, where I have had heap based objects in main, is not for the main application, but for test applications…
Or for when the object under test/use, is created via a shared library, and its interface returns the pointer.
Scott
From: interest-bounces+scott.bloom=onshorecs.com at qt-project.org [mailto:interest-bounces+scott.bloom=onshorecs.com at qt-project.org] On Behalf Of Constantin Makshin
Sent: Saturday, January 11, 2014 3:24 AM
To: Qt Interest
Subject: Re: [Interest] Create the main widget on heap in main()
True, but why would one need a parented object in main()? :-)
On Jan 11, 2014 3:13 PM, "Etienne Sandré-Chardonnal" <etienne.sandre at m4x.org<mailto:etienne.sandre at m4x.org>> wrote:
QObjects with a parent should never be allocated on stack (otherwise, you delete them twice)
2014/1/11 Constantin Makshin <cmakshin at gmail.com<mailto:cmakshin at gmail.com>>
Saving some stack space may make sense on mobile platforms, but in general yes, creating objects on the heap in main() is pretty much the same thing as creating them on the stack.
On Jan 11, 2014 2:02 PM, "Alejandro Exojo" <suy at badopi.org<mailto:suy at badopi.org>> wrote:
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
_______________________________________________
Interest mailing list
Interest at qt-project.org<mailto:Interest at qt-project.org>
http://lists.qt-project.org/mailman/listinfo/interest
_______________________________________________
Interest mailing list
Interest at qt-project.org<mailto: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/20140111/15014bdf/attachment.html>
More information about the Interest
mailing list