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. Multiple actions on right click on label

Multiple actions on right click on label

Scheduled Pinned Locked Moved Solved General and Desktop
actionright-clicklabel
3 Posts 3 Posters 412 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.
  • B Offline
    B Offline
    BigBen
    wrote on last edited by
    #1

    Hi.

    I am using the following code to right click on a label and see a context menu:

    void MainWindow::ShowContextMenu(const QPoint& pos)
    {
        QLabel* mylabel = qobject_cast<QLabel*>(sender());
        QPoint globalPos = mylabel->mapToGlobal(pos);
    
        QMenu myMenu;
        myMenu.addAction("Hide");
        //myMenu.addAction("Highlight");
        QAction* selectedItem = myMenu.exec(globalPos);
        if (selectedItem)
        {
           qInfo()<<"Hide works!";
        }
        else{
        //nothing chosen
        }
    }
    

    This works fine for one item in the menu (Hide). However, if I want to add another item to the menu, how can I do so?

    I can add the item using addAction, but not able to understand how to execute some action if it is clicked.

    jsulmJ JonBJ 2 Replies Last reply
    0
    • B BigBen

      Hi.

      I am using the following code to right click on a label and see a context menu:

      void MainWindow::ShowContextMenu(const QPoint& pos)
      {
          QLabel* mylabel = qobject_cast<QLabel*>(sender());
          QPoint globalPos = mylabel->mapToGlobal(pos);
      
          QMenu myMenu;
          myMenu.addAction("Hide");
          //myMenu.addAction("Highlight");
          QAction* selectedItem = myMenu.exec(globalPos);
          if (selectedItem)
          {
             qInfo()<<"Hide works!";
          }
          else{
          //nothing chosen
          }
      }
      

      This works fine for one item in the menu (Hide). However, if I want to add another item to the menu, how can I do so?

      I can add the item using addAction, but not able to understand how to execute some action if it is clicked.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @BigBen Just add another action but remember the instances, call exec and check what it returns:

      QMenu myMenu;
      QAction hide("Hide");
      QAction highlight("Highlight");
      myMenu.addAction(&hide);
      myMenu.addAction(&highlight);
      QAction* selectedItem = myMenu.exec();
      if (selectedItem == &hide)
      {
             qInfo()<<"Hide works!";
      } else if (selectedItem == &highlight) {
          qInfo()<<"Highlight works!";
      }  else{
          //nothing chosen
      }
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • B BigBen

        Hi.

        I am using the following code to right click on a label and see a context menu:

        void MainWindow::ShowContextMenu(const QPoint& pos)
        {
            QLabel* mylabel = qobject_cast<QLabel*>(sender());
            QPoint globalPos = mylabel->mapToGlobal(pos);
        
            QMenu myMenu;
            myMenu.addAction("Hide");
            //myMenu.addAction("Highlight");
            QAction* selectedItem = myMenu.exec(globalPos);
            if (selectedItem)
            {
               qInfo()<<"Hide works!";
            }
            else{
            //nothing chosen
            }
        }
        

        This works fine for one item in the menu (Hide). However, if I want to add another item to the menu, how can I do so?

        I can add the item using addAction, but not able to understand how to execute some action if it is clicked.

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

        @BigBen
        When you go QMenu::addAction("SomeString") it creates a QAction for you, and returns it. And QMenu::exec() returns the QAction which was clicked. So you could check which one is returned and act on it, like:

        QAction *hideAction = myMenu.addAction("Hide");
        QAction *showAction = myMenu.addAction("Show");
        QAction* selectedAction = myMenu.exec(globalPos);
        if (selectedAction == hideAction)
            qInfo() << "Hide";
        else if (selectedAction == showAction)
            qInfo() << "Show";
        

        Alternatively, when you create the QActions you could attach a slot to its triggered signal:

        connect(hideAction, &QAction::triggered, this, []() { qInfo() << "Hide"; } );
        
        1 Reply Last reply
        3

        • Login

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