Checkable QAction doesn't change its state
-
I don't know what is wrong
I use this code to create checkable QActionvoid MainWindow::showContextMenu(const QPoint &p) { qDebug() << "hit here"; myMenu = new QMenu(); testAction = myMenu->addAction("testAction"); testAction->setCheckable(true); connect(testAction, &QAction::triggered, this, [=](bool checked){qDebug()<< checked;}); myMenu->exec(mapToGlobal(p)); }
I can open the context menu but when I click on the action its state never changes either visually or the debugged message. What I am missing here?
-
I don't know what is wrong
I use this code to create checkable QActionvoid MainWindow::showContextMenu(const QPoint &p) { qDebug() << "hit here"; myMenu = new QMenu(); testAction = myMenu->addAction("testAction"); testAction->setCheckable(true); connect(testAction, &QAction::triggered, this, [=](bool checked){qDebug()<< checked;}); myMenu->exec(mapToGlobal(p)); }
I can open the context menu but when I click on the action its state never changes either visually or the debugged message. What I am missing here?
@abdoemr11
Your code looks correct to me. (You might put intestAction->setChecked(true);
aftersetCheckable(true)
to make sure the checkbox actually works.) Next step is what platform and which version of Qt, you could search the Qt bugs for any reports of it not working in certain environment? -
I tried testAction->setChecked(true); and it make the action checked but again it doesn't change after that.
I tried also the Menu Example and it work just fine!!@abdoemr11
Which "Menu Example" is that --- what's the link? -
I don't know what is wrong
I use this code to create checkable QActionvoid MainWindow::showContextMenu(const QPoint &p) { qDebug() << "hit here"; myMenu = new QMenu(); testAction = myMenu->addAction("testAction"); testAction->setCheckable(true); connect(testAction, &QAction::triggered, this, [=](bool checked){qDebug()<< checked;}); myMenu->exec(mapToGlobal(p)); }
I can open the context menu but when I click on the action its state never changes either visually or the debugged message. What I am missing here?
@abdoemr11 said in Checkable QAction doesn't change its state:
myMenu = new QMenu();
You create a new menu each time, then testAction is a different object ;)
-
@abdoemr11 said in Checkable QAction doesn't change its state:
myMenu = new QMenu();
You create a new menu each time, then testAction is a different object ;)
@mpergand
I don't understand. Yes, he creates a newQMenu
, with a newQAction
, each time inshowContextMenu()
. Apart from the fact that this (may?) leak aQMenu
/QAction
, how does this affect that which one he is currently showing (i.e. the latest createdtestAction
which getsconnect()
ed) does not respond to check-clicks?? -
@abdoemr11
Which "Menu Example" is that --- what's the link? -
bool menuState=false; // instance variable of MainWindow void MainWindow::showContextMenu(const QPoint &p) { qDebug() << "hit here"; QMenu myMenu; auto testAction = myMenu.addAction("testAction"); testAction->setCheckable(true); testAction->setChecked(menuState); connect(testAction, &QAction::triggered, this, [=](bool checked) {qDebug()<< checked; menuState=checked;}); myMenu.exec(mapToGlobal(p)); }
-
@mpergand
I don't understand. Yes, he creates a newQMenu
, with a newQAction
, each time inshowContextMenu()
. Apart from the fact that this (may?) leak aQMenu
/QAction
, how does this affect that which one he is currently showing (i.e. the latest createdtestAction
which getsconnect()
ed) does not respond to check-clicks??@JonB said in Checkable QAction doesn't change its state:
how does this affect that which one he is currently showing (i.e. the latest created testAction which gets connect()ed) does not respond to check-clicks??
It responds, although the debug msg reflets the state change, it's not the state of the new created action upon the next click , so it prints always the same state (true if the state of the action is unchecked and vice-versa).
Actually I don't understand what you don't understand ;)
-
@abdoemr11 said in Checkable QAction doesn't change its state:
myMenu = new QMenu();
You create a new menu each time, then testAction is a different object ;)
@mpergand Actually it was the problem thanks for the hint
The problem was that the main code base was using this approach (creating the action each time the context menu is shown) it works without problem with ordinary action but checkable action needs to initialized once. -
@JonB said in Checkable QAction doesn't change its state:
how does this affect that which one he is currently showing (i.e. the latest created testAction which gets connect()ed) does not respond to check-clicks??
It responds, although the debug msg reflets the state change, it's not the state of the new created action upon the next click , so it prints always the same state (true if the state of the action is unchecked and vice-versa).
Actually I don't understand what you don't understand ;)
@mpergand said in Checkable QAction doesn't change its state:
Actually I don't understand what you don't understand ;)
@abdoemr11 said in Checkable QAction doesn't change its state:
it works without problem with ordinary action but checkable action needs to initialized once.
I had no idea the OP was wanting to repeat showing an action which had to carry its check state forward from one invocation to a later one! I thought he couldn't get the checkbox to check.