1
    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?

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
user2775042
  • 509
  • 2
  • 6
  • 21

1 Answers1

2

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
}
Mitch
  • 23,716
  • 9
  • 83
  • 122
  • Thanks for that. but i keep getting the error - no matching function for call to 'connect' I have changed the parameters in both header and class file. How can i fix it please? there is no triggered in my menu. My menu is in a table – user2775042 Nov 05 '15 at 07:44
  • Please show me your connect statement and the exact error message. – Mitch Nov 05 '15 at 07:54
  • statement - connect(myMenu, SIGNAL(triggered(QAction*)), this, SLOT(menuClicked(QAction*))); 159: error: no matching member function for call to 'connect' connect(myMenu, SIGNAL(triggered(QAction*)), this, SLOT(menuClicked(QAction*))); ^~~~~~~ – user2775042 Nov 05 '15 at 07:55
  • Is `myMenu` a pointer? If not, you'll have to use `&myMenu` instead. I'm not sure what you mean by `there is no triggered in my menu`. Is the menu not a `QMenu`? – Mitch Nov 05 '15 at 07:57
  • yeah it wasnt a pointer thats why it wasnt working. Thanks so much – user2775042 Nov 05 '15 at 08:00