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

程梁 devbean at outlook.com
Sat Jan 11 00:55:54 CET 2014


Thank you for your reply!
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?

Cheng Liang
Nanjing, China
http://www.devbean.net

From: konrad at silmor.de
To: interest at qt-project.org
Date: Fri, 10 Jan 2014 14:33:15 +0100
Subject: Re: [Interest] Create the main widget on heap in main()



Hi,
 
On Friday 10 January 2014, 程梁 wrote:
> 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?
 
In this particular case it should not present a problem because of the mechanism how app.exec() returns: when the last visible widget is closed the main event loop (i.e. app.exec()) exits and main ends (also killing the app object). Meaning: all widgets are already invisible. Otherwise, if it is killed with qApp->close(), the last action that QApplication takes is to unmap (close) all windows.
 
Of course the code would be cleaner if you told the QLabel to kill itself on closure:
 
label->setAttribute(Qt::WA_DeleteOnClose);
 
 
 
	Konrad
_______________________________________________
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/1d06293a/attachment.html>


More information about the Interest mailing list