CheckBox in qactions not working
-
I am trying to have a QAction in context menu, where I toggle the state of the check box using the following code. But, it is not working. I have also tried checking the box in the beginning (so it does show checked) but it doesn't get unchecked when I click the updateOn action.
Any suggestions would be greatly appreciated. Thanks in advance
# author = copied from somewhere from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu import sys class Window(QMainWindow): def __init__(self): super().__init__() self.title = "PyQt5 Context Menu" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.InitWindow() def InitWindow(self): self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.show() def contextMenuEvent(self, event): contextMenu = QMenu(self) updateOn = contextMenu.addAction("Update On") updateOn.setCheckable(True) updateOn.setChecked(True) quitAct = contextMenu.addAction("Quit") action = contextMenu.exec_(self.mapToGlobal(event.pos())) if action == updateOn: print("action=", action) if updateOn.isChecked(): updateOn.setChecked(False) else: updateOn.setChecked(True) if action == quitAct: self.close() App = QApplication(sys.argv) window = Window() sys.exit(App.exec())
-
-
I just realized, your action handling is not needed.
Connect quitAct to close and for updateOn, if the action is checkable, then clicking on it will toggle it so there's nothing more to do.
-
Hi,
You create a new menu each time that function is called hence the state is not persisted.
On a side note, don't call show when initializing a widget. It's not its role to show itself, it's the one from code that manages it.
-
@SGaist
Oops, thanks for correcting me.Now, it does get checked and unchecked but it works if I do setChecked(True) when isChecked()==True, which is baffling me. As I would assume I would setChecked(True) when isChecked==False and vice-versa.
Can you please also tell me why would the checkbox behave weirdly for the following conditional block?
Oh yeah, I moved removed .show() from init. Thanks for that suggestion.
# author = Jaykumar Vaidya from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu import sys class Window(QMainWindow): def __init__(self): super().__init__() self.title = "PyQt5 Context Menu" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.InitWindow() self.contextMenu = QMenu(self) self.updateOn = self.contextMenu.addAction("Update On") self.openAct = self.contextMenu.addAction("Open") self.quitAct = self.contextMenu.addAction("Quit") self.updateOn.setCheckable(True) self.updateOn.setChecked(False) def InitWindow(self): self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) def contextMenuEvent(self, event): action = self.contextMenu.exec_(self.mapToGlobal(event.pos())) if action == self.updateOn: print("action=", action) print(self.updateOn.isChecked()) if self.updateOn.isChecked(): self.updateOn.setChecked(True) else: self.updateOn.setChecked(False) print(self.updateOn.isChecked()) if action == self.quitAct: self.close() App = QApplication(sys.argv) window = Window() window.show() sys.exit(App.exec())
-
I just realized, your action handling is not needed.
Connect quitAct to close and for updateOn, if the action is checkable, then clicking on it will toggle it so there's nothing more to do.
-