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
Qt 6.11 is out! See what's new in the release blog

Context Menu Event

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.0k 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, 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?

    JonBJ 1 Reply Last reply
    0
    • M Martinuccia_96

      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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #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
      0
      • JonBJ JonB

        @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 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
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #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
          3
          • VRoninV VRonin

            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 last edited by
            #5

            THANKS SO MUCH!

            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