Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ownership of QAction

When adding a QAction* to a QMenu who is responsible for deleting the QAction* object? I couldn't find it mentioned in the documentation for QMenu or QAction.

void MyClass::contextMenuEvent(QContextMenuEvent *evt)
{
    QMenu menu(this);
    QAction *a = new QAction(tr("Some action"), this);
    menu.addAction(a); // who owns a?
    menu.exec(evt->globalPos());
}

The Qt Menus example doesn't delete any of the actions it creates so I assume that QMenu takes ownership of the QAction. Is that correct?

like image 358
glennr Avatar asked Jan 05 '12 22:01

glennr


1 Answers

If you add a pre-existing action to a QWidget (which QMenu is) then:

The ownership of action is not transferred to this QWidget.

Note that in your example, deletion of a is handled by MyClass because you have used it as a parent QObject, so a is deleted in QObject's destructor.

like image 195
cmannett85 Avatar answered Sep 27 '22 17:09

cmannett85