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
QtWS25 Last Chance

Multiple actions on right click on label

Scheduled Pinned Locked Moved Solved General and Desktop
actionright-clicklabel
3 Posts 3 Posters 403 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 20 Jun 2022, 02:31 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.

    J J 2 Replies Last reply 20 Jun 2022, 07:57
    0
    • B BigBen
      20 Jun 2022, 02:31

      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.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 20 Jun 2022, 07:57 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
        20 Jun 2022, 02:31

        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.

        J Offline
        J Offline
        JonB
        wrote on 20 Jun 2022, 08:00 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

        1/3

        20 Jun 2022, 02:31

        • Login

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