[SOLVED] QTextEdit createStandardContextMenu



  • Hi all,
    I have a main window with a menu bar with an empty menu (let's call it mnEditCmds).

    I aslo have a dockWidget with a QTextEdit: when I right-click on it I show a custom context menu that has all the actions in the standardContextMenu plus others that I add manually (using the customContextMenuRequested signal), like this:

    @void MainWin::on_textEd_SCL_customContextMenuRequested(const QPoint &pos)
    {
    QMenu *scMn = m_UI->textEd_SCL->createStandardContextMenu(pos);

    QMenu *cMn = new QMenu();
    cMn->addMenu(m_UI->mnSoundFile);
    cMn->addSeparator();
    QMenu *mnEC = cMn->addMenu(m_UI->mnEditCmds->title());
    mnEC->addActions(scMn->actions());
    
    cMn->exec(m_UI->textEd_SCL->mapToGlobal(pos));
    
    delete(scMn);
    delete(cMn);
    

    }@

    What I want to do is to have all the actions of the standardContextMenu also in my mnEdit.
    So I use a slot on the aboutToShow signal, like this:

    @void MainWin::mnEditCmds_abouttoshow()
    {
    QMenu *mnEditCmds = m_UI->mnEditCmds;
    qDeleteAll(mnEditCmds->actions());

    QMenu *cMn = m_UI->textEd_SCL->createStandardContextMenu();
    // cMn->setAttribute(Qt::WA_DeleteOnClose);
    
    mnEditCmds->addActions(cMn->actions());
    delete(cMn);
    

    }@

    Now my problem is that if I delete cMn as in the code above, my mnEditCmds shows no child actions when I click on it, because I deleted them deleting cMn. Fine.

    But if I don't delete cMn and set its property WA_DeleteOnClose, will the code leak ? The context menu cMn is actually not shown, so I don't know if it gets a close signal (triggering its deletion). I'm actually under the impression it doesn't. So it leaks. Maybe :)

    What'd be the right way to do that ?

    MaX.


  • Moderators

    How about this (not tested)?
    @
    connect(mnEditCmds , &QMenu::aboutToHide, cMn, &QMenu::deleteLater);
    @



  • IT.JUST.WORKS. Brillant solution.

    Thanks Chris !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.