Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QTextEdit createStandardContextMenu
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QTextEdit createStandardContextMenu

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HornetMaX
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • H Offline
        H Offline
        HornetMaX
        wrote on last edited by
        #3

        IT.JUST.WORKS. Brillant solution.

        Thanks Chris !

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved