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 Event
Forum Update on Monday, May 27th 2025

Context Menu Event

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 645 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 9 Jun 2020, 11:49 last edited by
    #1

    Hi, everyone.
    I have a problem on the context menu Event.
    I have a window of this kind: Annotazione 2020-06-09 134358.png
    the white box on the left is a textEdit, while the right one is a QTreeWidget.
    I've implemented the context menu in the following way:

    void notepad::contextMenuEvent(QContextMenuEvent *)
    {
        QMenu submenu;
        QString style="QMenu {border-radius:15px; background-color: white;margin: 2px; border: 1px solid rgb(58, 80, 116); color:  rgb(58, 80, 116);}QMenu::separator {height: 2px;background: rgb(58, 80, 116);margin-left: 10px;margin-right: 5px;}";
        submenu.setStyleSheet(style);
        submenu.addAction(tr("Copy"),this,&notepad::on_actionCopy_triggered);
        submenu.addSeparator();
        submenu.addAction(tr("Cut"),this,&notepad::on_actionCut_triggered);
        submenu.addSeparator();
        submenu.addAction(tr("Paste"),this,&notepad::on_actionPaste_triggered);
        submenu.addSeparator();
        QPoint globalPos=ui->textEdit->cursor().pos();
        submenu.exec(globalPos);
    }
    
    

    Everything works, but I would like to show this menu only If I click with the mouse in the textEdit part and not in the part that shows a list of onlineUsers.
    How can I do it?

    J 1 Reply Last reply 9 Jun 2020, 12:12
    0
    • M Martinuccia_96
      9 Jun 2020, 11:49

      Hi, everyone.
      I have a problem on the context menu Event.
      I have a window of this kind: Annotazione 2020-06-09 134358.png
      the white box on the left is a textEdit, while the right one is a QTreeWidget.
      I've implemented the context menu in the following way:

      void notepad::contextMenuEvent(QContextMenuEvent *)
      {
          QMenu submenu;
          QString style="QMenu {border-radius:15px; background-color: white;margin: 2px; border: 1px solid rgb(58, 80, 116); color:  rgb(58, 80, 116);}QMenu::separator {height: 2px;background: rgb(58, 80, 116);margin-left: 10px;margin-right: 5px;}";
          submenu.setStyleSheet(style);
          submenu.addAction(tr("Copy"),this,&notepad::on_actionCopy_triggered);
          submenu.addSeparator();
          submenu.addAction(tr("Cut"),this,&notepad::on_actionCut_triggered);
          submenu.addSeparator();
          submenu.addAction(tr("Paste"),this,&notepad::on_actionPaste_triggered);
          submenu.addSeparator();
          QPoint globalPos=ui->textEdit->cursor().pos();
          submenu.exec(globalPos);
      }
      
      

      Everything works, but I would like to show this menu only If I click with the mouse in the textEdit part and not in the part that shows a list of onlineUsers.
      How can I do it?

      J Offline
      J Offline
      JonB
      wrote on 9 Jun 2020, 12:12 last edited by JonB 6 Sept 2020, 12:20
      #2

      @Martinuccia_96
      You want to place the context menu on the text edit, not on the whole window (notepad?). QTextEdit has its own dedicated context menu support --- see https://doc.qt.io/qt-5/qtextedit.html#contextMenuEvent & https://doc.qt.io/qt-5/qtextedit.html#createStandardContextMenu. The other way would be to test the position of the QContextMenuEvent in your code and only pop up the menu if it's over the QTextEdit, but I think the former approach seems more appropriate/convenient.

      M 1 Reply Last reply 9 Jun 2020, 12:36
      0
      • J JonB
        9 Jun 2020, 12:12

        @Martinuccia_96
        You want to place the context menu on the text edit, not on the whole window (notepad?). QTextEdit has its own dedicated context menu support --- see https://doc.qt.io/qt-5/qtextedit.html#contextMenuEvent & https://doc.qt.io/qt-5/qtextedit.html#createStandardContextMenu. The other way would be to test the position of the QContextMenuEvent in your code and only pop up the menu if it's over the QTextEdit, but I think the former approach seems more appropriate/convenient.

        M Offline
        M Offline
        Martinuccia_96
        wrote on 9 Jun 2020, 12:36 last edited by
        #3

        @JonB Yes, I want to do it.. I didn't use the standardContextMenu because I needed to modify it.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VRonin
          wrote on 9 Jun 2020, 13:05 last edited by VRonin 6 Sept 2020, 13:07
          #4

          This is not the usual way to do it, you don't need to reimplement an event.

          1. add QMenu *m_textEditContextMenu; as a private member of notepad
          2. in the constructor of notepad add
              ui->textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
              connect(ui->textEdit,&QWidget::customContextMenuRequested,this,&notepad::showTextEditContextMenu);
          
              m_textEditContextMenu=new QMenu(this);
              const auto stylesheet=QStringLiteral("QMenu {border-radius:15px; background-color: white;margin: 2px; border: 1px solid rgb(58, 80, 116); color:  rgb(58, 80, 116);}QMenu::separator {height: 2px;background: rgb(58, 80, 116);margin-left: 10px;margin-right: 5px;}");
              m_textEditContextMenu->setStyleSheet(stylesheet);
              m_textEditContextMenu->addAction(tr("Copy"),this,&notepad::on_actionCopy_triggered);
              m_textEditContextMenu->addSeparator();
              m_textEditContextMenu->addAction(tr("Cut"),this,&notepad::on_actionCut_triggered);
              m_textEditContextMenu->addSeparator();
              m_textEditContextMenu->addAction(tr("Paste"),this,&notepad::on_actionPaste_triggered);
              m_textEditContextMenu->addSeparator();
          
          1. create a private slot
          void showTextEditContextMenu(const QPoint &pos){
          m_textEditContextMenu->popup(ui->textEdit->mapToGlobal(pos));
          }
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          M 1 Reply Last reply 9 Jun 2020, 14:37
          3
          • V VRonin
            9 Jun 2020, 13:05

            This is not the usual way to do it, you don't need to reimplement an event.

            1. add QMenu *m_textEditContextMenu; as a private member of notepad
            2. in the constructor of notepad add
                ui->textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
                connect(ui->textEdit,&QWidget::customContextMenuRequested,this,&notepad::showTextEditContextMenu);
            
                m_textEditContextMenu=new QMenu(this);
                const auto stylesheet=QStringLiteral("QMenu {border-radius:15px; background-color: white;margin: 2px; border: 1px solid rgb(58, 80, 116); color:  rgb(58, 80, 116);}QMenu::separator {height: 2px;background: rgb(58, 80, 116);margin-left: 10px;margin-right: 5px;}");
                m_textEditContextMenu->setStyleSheet(stylesheet);
                m_textEditContextMenu->addAction(tr("Copy"),this,&notepad::on_actionCopy_triggered);
                m_textEditContextMenu->addSeparator();
                m_textEditContextMenu->addAction(tr("Cut"),this,&notepad::on_actionCut_triggered);
                m_textEditContextMenu->addSeparator();
                m_textEditContextMenu->addAction(tr("Paste"),this,&notepad::on_actionPaste_triggered);
                m_textEditContextMenu->addSeparator();
            
            1. create a private slot
            void showTextEditContextMenu(const QPoint &pos){
            m_textEditContextMenu->popup(ui->textEdit->mapToGlobal(pos));
            }
            
            M Offline
            M Offline
            Martinuccia_96
            wrote on 9 Jun 2020, 14:37 last edited by
            #5

            THANKS SO MUCH!

            1 Reply Last reply
            0

            1/5

            9 Jun 2020, 11:49

            • Login

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