[Qt-interest] show a widget without a main window

Malyushytsky, Alex alex at wai.com
Thu Mar 5 06:40:45 CET 2009


Qt::WA_DeleteOnClose is not set by default. It would be inconvenient because major number of widgets do not need to delete themselves when closed. They will be deleted either when parent widget is destroyed or when object is going out of scope in case of allocation on the stack.

According to my knowledge QApplication does not do  any behind-the-scene deleting, cause you have to do it yourself in the all possible ways.

So QMainWindow or any other QWidget allocated in the main in such manner will cause memory leaks if not deleted. When and how they should be deleted depends on your application. That was a reason why I mentioned most often used ways.
"myWidget" class from the code snippet could set Qt::WA_DeleteOnClose attribute in constructor, so it would not be a memory leak.
If nothing is done, the memory will leak on the platforms which don't clean after application when it exits.

Best regards,
  Alex





-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of knowledge seeker
Sent: Wednesday, March 04, 2009 9:02 PM
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] show a widget without a main window

Its not that - "what can be done", I was just curious that the code
snippet had a memory leak or not, by just looking at it at one glance
(considering there are no code-tweaks occuring behind the scenes).

Or, shall I presume that Qt::WA_DeleteOnClose attribute is set-as-default ?

Regards,
Knowledge Seeker


On Thu, Mar 5, 2009 at 9:21 AM, Malyushytsky, Alex <alex at wai.com> wrote:
> QApplication can't be parent object for widget, cause it is not a widget.
>
> There is no difference in the memory management in the code when you have main window or you have any other widget shown instead.
>
> For example you can set Qt::WA_DeleteOnClose attribute with QWidget::setAttribute(), so widget will delete itself when closed or you can do it somewhere as a result of lastWindowClosed () or just allocate your widget on a stack.
>
> Hope this helps,
>    Alex
>
> -----Original Message-----
> From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Knowledge Seeker
> Sent: Wednesday, March 04, 2009 5:43 PM
> To: qt-interest at trolltech.com
> Subject: Re: [Qt-interest] show a widget without a main window
>
> Is not there a memory leak? there is no
> delete w;
> And also object 'w' is not linked to the parentobject - QApplication, so
> it cant be automatically deleted !!
>
>
> Regards,
> Knowledge Seeker
>
> Malyushytsky, Alex wrote:
>>
>> Most of Qt examples are not using main window. Example look like this:
>>
>> #include <QtGui>
>>
>> #include "myWidget.h"
>>
>> //! [main function]
>>
>> int main(int argc, char *argv[])
>>
>> {
>>
>> QApplication app(argc, argv);
>>
>> myWidget *w = new myWidget;
>>
>> w->show();
>>
>> return app.exec();
>>
>> }
>>
>> Regards
>>
>> Alex Malyushyutsky
>>
>> ------------------------------------------------------------------------
>>
>> *From:* qt-interest-bounces at trolltech.com
>> [mailto:qt-interest-bounces at trolltech.com] *On Behalf Of
>> *ri at eeda.denso.co.jp
>> *Sent:* Tuesday, March 03, 2009 8:56 PM
>> *To:* qt-interest at trolltech.com
>> *Subject:* [Qt-interest] show a widget without a main window
>>
>> Hi
>>
>> I would like to know that if it's possible to show a widget without a
>> main window.
>>
>> For example, I want to show a QTableWidget, but only a QTableWidget,
>>
>> not a QTableWidget inside a QMainWindow.
>>
>> Is it possible? if yes, any sample code?
>>
>> Thanks in advance!
>>
>> Shiniji,Mizuki
>>
>>
>>
>> ---------------------------------------------------------------------------------------------------
>> Weidlinger Associates, Inc. made the following annotations.
>>
>> "This message and any attachments are solely for the intended
>> recipient and may contain confidential or privileged information. If
>> you are not the intended recipient, any disclosure, copying, use, or
>> distribution of the information included in this message and any
>> attachments is prohibited. If you have received this communication in
>> error, please notify us by reply e-mail and immediately and
>> permanently delete this message and any attachments. Thank you."
>>
>> "Please consider our environment before printing this email."
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
> "This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you."
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>

_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest

"This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you."




More information about the Qt-interest-old mailing list