Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, June 2007
Not quite sure I understand memory management in Qt


Message 1 in thread

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? What if I keep right clicking, do 
new QMenus keep being created and not destroyed? 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?

Cesar


--
 [ signature omitted ] 

Message 2 in thread

Hi cesar,

I had the very same doubts as you when I started working with Qt. But 
rest yourself assured: Whenever Qt provides to option of setting a 
parent - set it and you won't have to worry about memory. That's an easy 
rule of thumb I stick to and I never had any problem with that.

Regards,
Thomas

cesar del solar schrieb:
> 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? What if I keep right clicking, do 
> new QMenus keep being created and not destroyed? 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?
> 
> Cesar
> 
> 
> --
> 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/
> 
> 

--
 [ signature omitted ] 

Message 3 in thread

So in this case, I should do  QMenu* contextMenu = new QMenu(this);    ?

Thanks for your help,

Cesar

"Thomas Dähling" <t.daehling@xxxxxxxxxxxxxx> wrote in message 
news:46672BB4.1020106@xxxxxxxxxxxxxxxxx
> Hi cesar,
>
> I had the very same doubts as you when I started working with Qt. But rest 
> yourself assured: Whenever Qt provides to option of setting a parent - set 
> it and you won't have to worry about memory. That's an easy rule of thumb 
> I stick to and I never had any problem with that.
>
> Regards,
> Thomas
>
> cesar del solar schrieb:
>> 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? What if I keep right clicking, 
>> do new QMenus keep being created and not destroyed? 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?
>>
>> Cesar
>>
>>
>> --
>> 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/ 


--
 [ signature omitted ] 

Message 4 in thread

Exactly. Just like Chris Burke pointed out :)

cesar del solar schrieb:
> So in this case, I should do  QMenu* contextMenu = new QMenu(this);    ?
> 
> Thanks for your help,
> 
> Cesar
> 
> "Thomas Dähling" <t.daehling@xxxxxxxxxxxxxx> wrote in message 
> news:46672BB4.1020106@xxxxxxxxxxxxxxxxx
>> Hi cesar,
>>
>> I had the very same doubts as you when I started working with Qt. But rest 
>> yourself assured: Whenever Qt provides to option of setting a parent - set 
>> it and you won't have to worry about memory. That's an easy rule of thumb 
>> I stick to and I never had any problem with that.
>>
>> Regards,
>> Thomas
>>
>> cesar del solar schrieb:
>>> 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? What if I keep right clicking, 
>>> do new QMenus keep being created and not destroyed? 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?
>>>
>>> Cesar
>>>
>>>
>>> --
>>> 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/ 
> 
> 
> --
> 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/
> 
> 

--
 [ signature omitted ] 

Message 5 in thread

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? What if I keep right clicking, do 
> new QMenus keep being created and not destroyed? 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?
>
> Cesar
>
>
> --
> 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/
>   
If a QObject does not have a parent and is created on the heap, you are 
responsible for deleting it. If you give the QObject a parent, the 
parent will take care of deleting it (provided you delete the parent).

-- 
 [ signature omitted ] 

Message 6 in thread

If I give the contextMenu a parent of "this", does this still means that 
multiple copies of the menu will be created every time I right-click, since 
the parent is not being deleted until the program closes?

Cesar

"Chris Burke" <cburke@xxxxxxx> wrote in message 
news:46672C1B.8020003@xxxxxxxxxx
> 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? What if I keep right clicking, 
>> do new QMenus keep being created and not destroyed? 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?
>>
>> Cesar
>>
>>
>> --
>> 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/
>>
> If a QObject does not have a parent and is created on the heap, you are 
> responsible for deleting it. If you give the QObject a parent, the parent 
> will take care of deleting it (provided you delete the parent).
>
> -- 
> chris burke
> phone: 617-621-0060 x195
> email: cburke@xxxxxxx
> skype: chris.burke0
>
> --
> 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/ 


--
 [ signature omitted ] 

Message 7 in thread

On 06.06.07 15:40:56, cesar del solar wrote:
> If I give the contextMenu a parent of "this", does this still means that 
> multiple copies of the menu will be created every time I right-click, since 
> the parent is not being deleted until the program closes?

Yes.

Andreas

-- 
 [ signature omitted ] 

Message 8 in thread

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

-- 
 [ signature omitted ] 

Message 9 in thread

On Thursday 07 June 2007 00:45, Andreas Pakulat wrote:
> 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.


As an alternative, you could delete it right away (or put it on the stack), 
using exec() instead of popup(). Since exec() returns a pointer to the 
triggered action, you wouldn't even need a signal/slot connection.

void contentsTableWidget::contextMenuEvent(QContextMenuEvent *event)
{
     QMenu contextMenu;
     contextMenu.addAction("action 1");
     contextMenu.addAction("action 2");
     menuItemClicked(contextMenu.exec(event->globalPos()));
}


or


void contentsTableWidget::contextMenuEvent(QContextMenuEvent *event)
{
     QMenu contextMenu;
     QAction *action1 = contextMenu.addAction("action 1");
     QAction *action2 = contextMenu.addAction("action 2");
     QAction *triggered = contextMenu.exec(event->globalPos());
     if (triggered == action1)
	...
     else if (triggered == action2) 
        ...
}


Matthias

--
 [ signature omitted ] 

Message 10 in thread

Another alternative is to not use a menu at all, but to add your context-menu 
actions to the widget (using QWidget::addAction()), and then setting the 
context menu policy to Qt::ActionsContextMenu using 
QWidget::setContextMenuPolicy()


Matthias

--
 [ signature omitted ] 

Message 11 in thread

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

-- 
 [ signature omitted ] 

Message 12 in thread

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/ 


--
 [ signature omitted ]