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 992 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 29 May 2020, 13:39 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 J 2 Replies Last reply 29 May 2020, 14:01
    0
    • M Martinuccia_96
      29 May 2020, 13:39

      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 29 May 2020, 14:01 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.

      J 1 Reply Last reply 29 May 2020, 14:30
      3
      • M Martinuccia_96
        29 May 2020, 13:39

        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");
        }
        
        J Offline
        J Offline
        JonB
        wrote on 29 May 2020, 14:02 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
          29 May 2020, 14:01

          @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.

          J Offline
          J Offline
          JonB
          wrote on 29 May 2020, 14:30 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 29 May 2020, 14:41
          1
          • J JonB
            29 May 2020, 14:30

            @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 29 May 2020, 14:41 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.

            J 1 Reply Last reply 29 May 2020, 14:57
            3
            • B Bonnie
              29 May 2020, 14:41

              @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.

              J Offline
              J Offline
              JonB
              wrote on 29 May 2020, 14:57 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
              • J JonB referenced this topic on 30 Oct 2024, 15:37

              4/6

              29 May 2020, 14:30

              • Login

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