1

I have a context menu with a list of item actions. By default the first action in the menu is selected. This is set by using the setChecked() & setCheckable() properties of QAction. But I'm unable to to change the other actions to checked when any other item in the menu is selected. Here is my code:

  QAction* action = new QAction(subMenu);

     for(...)
     {
           action = subMenu->addAction(...);
           action->setData(...);
           action->setCheckable(true);
           if(i==1)
           {
             action->setChecked(true);
           }
     }
  connect(subMenu, SIGNAL(triggered(QAction *)),
        this, SLOT(onModeSelected(QAction *)), Qt::UniqueConnection);

void onModeSelected(Qaction* action)
{
    action->setchecked(true);
    ...
}
Beginner
  • 43
  • 4
  • I do not really understand why you create one `QAction` outside the `for`-loop with the menu as parent (which should create one entry) and then again in the loop. Did you debug the pointers to see if you get the correct action in the SLOT? – Bowdzone Nov 28 '14 at 07:31
  • @Bowdzone I had done that becasue when I did a setchecked() without allocating memory it crashed. Anway, I now changed my code and created the QAction variable inside the for loop. – Beginner Nov 28 '14 at 08:34
  • And did you debug the code to check if you received the correct pointer in your SLOT? Also, if you set a `QAction` to be 'checkable' you normaly do not need to perform the checking yourself. – Bowdzone Nov 28 '14 at 08:49
  • Yes I'm receiving the correct pointer and values change accordingly. Only the indication that the item is the selected is not happening. – Beginner Nov 28 '14 at 11:15
  • What do you mean by selected? If an item is clicked (=triggered) it should be checked automatically if it is checkable. If you mean the mouse hovers over it then the signal is called hovered() – Bowdzone Nov 28 '14 at 11:24
  • I meant the same. When an item is clicked, it is expected to be checked automatically if its checkable. That is not happening in my case currently. – Beginner Nov 28 '14 at 11:28
  • This will not work....Please create a [MCVE](http://stackoverflow.com/help/mcve) reproducing the issue and update your question with it. – Bowdzone Nov 28 '14 at 11:34

0 Answers0