PDA

View Full Version : QTableWidget will delete its QMenu twice?



liuyanghejerry
17th May 2011, 15:55
I have a piece of code like pseudo code below:


class ImIncludeTable{
mMainTable = new QTableWidget(30,4,this);//This class contains a QTableWidget
....
mAction1 = new QAction(tr("Book/Cancel"),mMainTable);
....
mMainTable->addAction(mAction1);//add an action to this QTableWidget
mMainTable->setContextMenuPolicy(Qt::ActionsContextMenu);
....
connect(mAction1,SIGNAL(trigger()),this,SLOT(OpenA Dialog));//Open a Dialog, like a Qt::ApplicationModal Dialog while the action triggered
....
if(OpenADialog->result()) delete this;//if the dialog says true, the object will be called to delete itself. It's not a direct call, but a call by other class.
}

When the code run to the delete object line, a runtime error pops up.
But if I don't use a right-clicked context, everything is fine.

So, will QTableWidget delete its QMenu twice?
Or, because the QMenu is deleted by itself(when disappearing) after the delete object call, so it is deleted twice?

Wong
17th May 2011, 16:15
delete this ?
Maybe you want call QObject::deleteLater () ?

Santosh Reddy
17th May 2011, 20:28
As Wong replied, it would be better to use QObject::deleteLater(), (in case you are able to it)

as far as I understand you can still use "delete this", as long as "this" (the object) is created on heap (Created using new operaator).
If it was created on stack (local variable, or class member variable), then the you application will raise runtime errors, as it will tried to be deleted twice, In this case even deleteLater() may also raise errors (Not sure of this though)