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. Context Menu
Forum Updated to NodeBB v4.3 + New Features

Context Menu

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.2k Views
  • 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.
  • M Offline
    M Offline
    Martinuccia_96
    wrote on last edited by
    #1

    Hi, I would like to change the standard context menu of QTextEdit. I've tried in the following way, but nothing happens:

    void notepad::contextMenuEvent(QContextMenuEvent *){
        QMenu *menu = ui->textEdit->createStandardContextMenu();
        menu->clear(); 
        menu->addAction("Save");  
        menu->addAction("Export to Pdf");
    }
    
    B JonBJ 2 Replies Last reply
    0
    • M Martinuccia_96

      Hi, I would like to change the standard context menu of QTextEdit. I've tried in the following way, but nothing happens:

      void notepad::contextMenuEvent(QContextMenuEvent *){
          QMenu *menu = ui->textEdit->createStandardContextMenu();
          menu->clear(); 
          menu->addAction("Save");  
          menu->addAction("Export to Pdf");
      }
      
      B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      @Martinuccia_96 You didn't show the menu...Use menu->popup(e->globalPos()) to show it.
      (As @JonB 's answer, exec() is easier for deleting the menu after it is done showing, I usually use popup() when it doesn't need to be deleted.)
      And if you use menu->clear() then no need to call createStandardContextMenu().
      You can create a menu as member variable, add actions in the constructor and just show it in the event.

      JonBJ 1 Reply Last reply
      3
      • M Martinuccia_96

        Hi, I would like to change the standard context menu of QTextEdit. I've tried in the following way, but nothing happens:

        void notepad::contextMenuEvent(QContextMenuEvent *){
            QMenu *menu = ui->textEdit->createStandardContextMenu();
            menu->clear(); 
            menu->addAction("Save");  
            menu->addAction("Export to Pdf");
        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Martinuccia_96
        You have to actually show the menu you have created! Just follow the example at https://doc.qt.io/qt-5/qtextedit.html#contextMenuEvent :

        void MyTextEdit::contextMenuEvent(QContextMenuEvent *event)
        {
            QMenu *menu = createStandardContextMenu();
            menu->addAction(tr("My Menu Item"));
            //...
            menu->exec(event->globalPos());
            delete menu;
        }
        
        1 Reply Last reply
        2
        • B Bonnie

          @Martinuccia_96 You didn't show the menu...Use menu->popup(e->globalPos()) to show it.
          (As @JonB 's answer, exec() is easier for deleting the menu after it is done showing, I usually use popup() when it doesn't need to be deleted.)
          And if you use menu->clear() then no need to call createStandardContextMenu().
          You can create a menu as member variable, add actions in the constructor and just show it in the event.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Bonnie said in Context Menu:

          (As @JonB 's answer, exec() is easier for deleting the menu after it is done showing, I usually use popup() when it doesn't need to be deleted.)

          Hi @Bonnie. You know I totally respect your answers, they are excellent, so I don't want to nitpick, but as someone said about this to you in another post the other day, doesn't it bother you that if you do it that way and with new QMenu(this) then you leave a new menu instance in existence every time the user clicks, which don't get deleted till you kill of the this?

          B 1 Reply Last reply
          1
          • JonBJ JonB

            @Bonnie said in Context Menu:

            (As @JonB 's answer, exec() is easier for deleting the menu after it is done showing, I usually use popup() when it doesn't need to be deleted.)

            Hi @Bonnie. You know I totally respect your answers, they are excellent, so I don't want to nitpick, but as someone said about this to you in another post the other day, doesn't it bother you that if you do it that way and with new QMenu(this) then you leave a new menu instance in existence every time the user clicks, which don't get deleted till you kill of the this?

            B Offline
            B Offline
            Bonnie
            wrote on last edited by
            #5

            @JonB Yes, I agreed with that. So I didn't argue.
            I was just expressing a concept that time, not the full code.
            Usually if I use new QMenu(this) everytime a menu shows, I'll add setAttribute(Qt::WA_DeleteOnClose) to it.
            This time, when I'm saying "doesn't need to be deleted", I mean the menu may be a member variable that can be used repeatedly and does not need to be deleted in every event.

            JonBJ 1 Reply Last reply
            3
            • B Bonnie

              @JonB Yes, I agreed with that. So I didn't argue.
              I was just expressing a concept that time, not the full code.
              Usually if I use new QMenu(this) everytime a menu shows, I'll add setAttribute(Qt::WA_DeleteOnClose) to it.
              This time, when I'm saying "doesn't need to be deleted", I mean the menu may be a member variable that can be used repeatedly and does not need to be deleted in every event.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Bonnie said in Context Menu:

              Usually if I use new QMenu(this) everytime a menu shows, I'll add setAttribute(Qt::WA_DeleteOnClose) to it.

              That's fine, that's what I wanted to know. Armed with that, I may be happier using popup() now! I had been thinking in term of having to add a deleteLater() on whatever "close" signal popup() (presumably) emits, this is much neater :)

              1 Reply Last reply
              0
              • JonBJ JonB referenced this topic on

              • Login

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