How to disable a QAction when variable x is equal to y?
-
I use QActions to simultaneously create buttons on my app's toolbar and keyboard shortcuts for the entire window since one of them hides the toolbar. One of these actions is not supposed to work if a certain variable x is equal to a certain value y. If this is the case, the button should be grayed out and the action unusable. I know about QAction's setEnabled slot, but I currently have two problems with using it:
- I don't know how to access a QAction outside of the initial setup. I've tried many permutations of
window.toolBar.actionName.setEnabled(False)
but none of them worked. Most of them give me an AttributeError: 'QToolBar' object has no attribute 'actionName'. Here's how I set it up:
actionIcon = QIcon.fromTheme('some-icon') actionName = QAction(QIcon(actionIcon), 'Go Back', self) actionName.setShortcut('Ctrl+A') actionName.triggered.connect(someFunction) self.toolBar.addAction(actionName), self.addAction(actionName)
- I don't know how to make a variable emit a signal when it's changed. I'm not sure if it's possible either. I haven't been able to find anything online.
I'm probably misunderstanding something here, but I've exhausted Google and my brain is fried, so I'm hoping someone knows a bit more about this than I do.
- I don't know how to access a QAction outside of the initial setup. I've tried many permutations of
-
Hi,
Keep a reference to that particular action so you can directly access it later.
Otherwise, you need to loop through all the actions associated with the toolbar until you find the right one. -
I use QActions to simultaneously create buttons on my app's toolbar and keyboard shortcuts for the entire window since one of them hides the toolbar. One of these actions is not supposed to work if a certain variable x is equal to a certain value y. If this is the case, the button should be grayed out and the action unusable. I know about QAction's setEnabled slot, but I currently have two problems with using it:
- I don't know how to access a QAction outside of the initial setup. I've tried many permutations of
window.toolBar.actionName.setEnabled(False)
but none of them worked. Most of them give me an AttributeError: 'QToolBar' object has no attribute 'actionName'. Here's how I set it up:
actionIcon = QIcon.fromTheme('some-icon') actionName = QAction(QIcon(actionIcon), 'Go Back', self) actionName.setShortcut('Ctrl+A') actionName.triggered.connect(someFunction) self.toolBar.addAction(actionName), self.addAction(actionName)
- I don't know how to make a variable emit a signal when it's changed. I'm not sure if it's possible either. I haven't been able to find anything online.
I'm probably misunderstanding something here, but I've exhausted Google and my brain is fried, so I'm hoping someone knows a bit more about this than I do.
@wayfarer said in How to disable a QAction when variable x is equal to y?:
- I don't know how to access a QAction outside of the initial setup. I've tried many permutations of
window.toolBar.actionName.setEnabled(False)
but none of them worked. Most of them give me an AttributeError: 'QToolBar' object has no attribute 'actionName'. Here's how I set it up:
actionName = QAction(QIcon(actionIcon), 'Go Back', self)
actionName
is a local variable which goes out of scope at the end of the function. Useself.actionName
if the goal is to create an attribute of the parent object which can be referenced as long as that object is valid.- I don't know how to make a variable emit a signal when it's changed. I'm not sure if it's possible either. I haven't been able to find anything online.
You might be find python properties useful. Implement the action enabling or disabling in the setter.
Another option is Qt properties, which support change signals. Unfortunately the documentation appears to be mistranslated from C++.
- I don't know how to access a QAction outside of the initial setup. I've tried many permutations of
-