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. Connect trigger() from a QMenu to another function

Connect trigger() from a QMenu to another function

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 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.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on last edited by Dummie1138
    #1

    Hi. I'm trying to connect trigger() from a QMenu to another function. This is the code.

    MenuBar::MenuBar(QWidget *parent) : QWidget(parent){
        resize(800, 20);
        QMenuBar *menuBar = new QMenuBar(this);
        qDebug() << menuBar->size();
        setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        QMenu *menuSettings = new QMenu("Settings", menuBar);
        menuBar->addMenu(menuSettings);
    
        QMenu *menuExit = new QMenu("Exit", menuSettings);
        menuSettings->addMenu(menuExit);
        connect(menuExit, QMenu::triggered(), this, [=](){this->onMenuExitTriggered();}); //THE IMPORTANT BIT
    }
    
    void MenuBar::onMenuExitTriggered(){
        qDebug() << "Exit";
    }
    

    On the connect line, the IDE is telling me I am calling a non-static member function with an object argument. This makes sense, since according to the QMenu documentation a QAction needs to be provided as the argument. However I am unsure how to represent this argument in syntax.

    Some of the code I have tried:

    connect(menuExit, QMenu::triggered(QAction::triggered()), this, [=](){this->onMenuExitTriggered();}); 
    

    (This is calling a non-static member function with an object argument, despite QAction's triggered not requiring an object argument, which is confusing me)

        QAction *actionExit = new QAction(menuExit);
    
        connect(actionExit, &QAction::triggered, this, [=](){this->onMenuExitTriggered();});
    

    (There are no syntax issues but I am unsure how to make the QAction a child of the QMenu menuExit)

    Please let me know if more info is required.

    JonBJ 1 Reply Last reply
    0
    • Dummie1138D Dummie1138

      Hi. I'm trying to connect trigger() from a QMenu to another function. This is the code.

      MenuBar::MenuBar(QWidget *parent) : QWidget(parent){
          resize(800, 20);
          QMenuBar *menuBar = new QMenuBar(this);
          qDebug() << menuBar->size();
          setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
          QMenu *menuSettings = new QMenu("Settings", menuBar);
          menuBar->addMenu(menuSettings);
      
          QMenu *menuExit = new QMenu("Exit", menuSettings);
          menuSettings->addMenu(menuExit);
          connect(menuExit, QMenu::triggered(), this, [=](){this->onMenuExitTriggered();}); //THE IMPORTANT BIT
      }
      
      void MenuBar::onMenuExitTriggered(){
          qDebug() << "Exit";
      }
      

      On the connect line, the IDE is telling me I am calling a non-static member function with an object argument. This makes sense, since according to the QMenu documentation a QAction needs to be provided as the argument. However I am unsure how to represent this argument in syntax.

      Some of the code I have tried:

      connect(menuExit, QMenu::triggered(QAction::triggered()), this, [=](){this->onMenuExitTriggered();}); 
      

      (This is calling a non-static member function with an object argument, despite QAction's triggered not requiring an object argument, which is confusing me)

          QAction *actionExit = new QAction(menuExit);
      
          connect(actionExit, &QAction::triggered, this, [=](){this->onMenuExitTriggered();});
      

      (There are no syntax issues but I am unsure how to make the QAction a child of the QMenu menuExit)

      Please let me know if more info is required.

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

      @Dummie1138 said in Connect trigger() from a QMenu to another function:

      connect(actionExit, &QAction::triggered, this, [=](){this->onMenuExitTriggered();});

      This is the only syntactically correct one (the QMenu::triggered(...) cannot be right, you will never want the parentheses, this must be the address of a member method like &QAction::triggered, not a call to that method which is what the (...) would do).

      IIRC, QAction *QMenu::menuAction() const returns a QMenu's action to use for the connection given a QMenu you have created (like from QMenu *menuExit = new QMenu("Exit", menuSettings)). UPDATE Or QAction *QMenu::addMenu(QMenu *menu) would return the QAction created from menuSettings->addMenu(menuExit).

      BTW, the difference between QAction::triggered and QMenu::triggered is that the former can be specified per item/action in a menu with multiple ones whereas the latter fires for any action/item on the menu, and you then have to find out which one if you need to know.

      1 Reply Last reply
      1
      • Dummie1138D Offline
        Dummie1138D Offline
        Dummie1138
        wrote on last edited by Dummie1138
        #3

        I have modified the code accordingly based on @JonB.

            connect(menuExit->menuAction(), &QAction::hovered, this, [=](){this->onMenuExitTriggered();});
        

        This works. Thank you very much to Jon for this.

        1 Reply Last reply
        1

        • Login

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