Search code examples
qtqmenuqaction

How to set signals for each action in QMenu?


    for(auto s :listofPossibleValues){
        // item =s;

        action = myMenu.addAction("Set Value to "+s);

        connect(action,SIGNAL(triggered(bool)),this,SLOT(menuClicked()));
    }
  void MainWindow::menuClicked(){
    value = new QStandardItem(item);
    model->setItem(mainindex->row(),mainindex->column(),value);
}

I add actions and connect signals to the slot to my menu using the code above. Previously I was using the item to be the text. But it will only work for last item.

Does anyone at least know how to get the action that I clicked on? How can I make it work for each individual item rather than just the last one?


Solution

  • Use the triggered signal of QMenu:

    connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(menuClicked(QAction*)));
    

    Then, in menuClicked():

    void MainWindow::menuClicked(QAction *action) {
        // do something with action
    }