qt-interest@trolltech.com
[Top] [All Lists]

Re: Not quite sure I understand memory management in Qt

Subject: Re: Not quite sure I understand memory management in Qt
From: "cesar del solar"
Date: Thu, 7 Jun 2007 13:44:40 -0700
that's actually exactly what i ended up doing, but thanks to everyone for 
all your help.

Cesar


"Dutton, Sam" <Sam.Dutton@xxxxxxxxx> wrote in message 
news:2A20E7FE73E166448E10EC7633C798F70E8D96AB@xxxxxxxxxxxxxxxx
> Why not just create the context menu in your contentsTableWidget
> constructor (or am I missing something?)
>
> Then the slot just looks more or less like this:
>
> void contentsTableWidget::contextMenuEvent(QContextMenuEvent *event) {
>    _contextMenu->popup(event->globalPos());
> }
>
> Sam Dutton
>
>
>
>
>
>
>
>
> SAM DUTTON
> SENIOR SITE DEVELOPER
>
> 200 GRAY'S INN ROAD
> LONDON
> WC1X 8XZ
> UNITED KINGDOM
> T +44 (0)20 7430 4496
> F
> E SAM.DUTTON@xxxxxxxxx
> WWW.ITN.CO.UK
>
> -----Original Message-----
>
> From: Andreas Pakulat [mailto:apaku@xxxxxx]
> Sent: Wednesday 06 June 2007 23:45
> To: qt-interest@xxxxxxxxxxxxx
> Subject: Re: Not quite sure I understand memory management in Qt
>
> On 06.06.07 14:39:26, cesar del solar wrote:
>> So I thought about this while writing this code but this applies to
> other
>> things I've done with Qt. I have a table widget and would like a pop
> up
>> menu, so I reimplemented the appropriate function:
>>
>> void contentsTableWidget::contextMenuEvent(QContextMenuEvent *event)
>> {
>>     QMenu* contextMenu = new QMenu();
>>     contextMenu->addAction("action 1");
>>     contextMenu->addAction("action 2");
>>     contextMenu->popup(event->globalPos());
>>     connect(contextMenu, SIGNAL(triggered(QAction*)), this,
>> SLOT(menuItemClicked(QAction*)));
>> }
>>
>> So when I right click the menu pops up, and clicking the actions works
> as
>> expected. But what happens to contextMenu?
>
> Nothing.
>
>> What if I keep right clicking, do new QMenus keep being created and
>> not destroyed?
>
> Right.
>
>> The way I dealt with this before is to create the QMenu* in the class
>> declaration, then inside this function at the beginning do something
>> like if (QMenu) delete QMenu, etc etc. Is this necessary?
>
> Yikes, why not do inside the contextMenuEvent():
>
> if(!m_menu)
> m_menu = new QMenu();
> m_menu->clear();
> m_menu->addAction(..);
> m_menu->popup(...);
>
> That way you reuse the existing object instead of allocating a new one
> every time for no good reason.
>
> Also I think it might make sense to do the connect before the popup,
> even though I doubt the user is fast enough to select an entry before
> popup returns it (IMHO) just makes more sense.
>
> Andreas
>
> -- 
> You will get what you deserve.
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
> Please Note:
>
>
>
> Any views or opinions are solely those of the author and do not 
> necessarily represent
> those of Independent Television News Limited unless specifically stated.
> This email and any files attached are confidential and intended solely for 
> the use of the individual
> or entity to which they are addressed.
> If you have received this email in error, please notify 
> postmaster@xxxxxxxxx
>
> Please note that to ensure regulatory compliance and for the protection of 
> our clients and business,
> we may monitor and read messages sent to and from our systems.
>
> Thank You.
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/ 


--
To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
"unsubscribe" in the subject or the body.
List archive and information: http://lists.trolltech.com/qt-interest/

<Prev in Thread] Current Thread [Next in Thread>