Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to disable a QAction when variable x is equal to y?
Forum Updated to NodeBB v4.3 + New Features

How to disable a QAction when variable x is equal to y?

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 3 Posters 180 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wayfarer
    wrote last edited by
    #1

    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:

    1. 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)
    
    1. 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.

    jeremy_kJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • W wayfarer

        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:

        1. 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)
        
        1. 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.

        jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote last edited by
        #3

        @wayfarer said in How to disable a QAction when variable x is equal to y?:

        1. 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. Use self.actionName if the goal is to create an attribute of the parent object which can be referenced as long as that object is valid.

        1. 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++.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply
        1
        • W Offline
          W Offline
          wayfarer
          wrote last edited by
          #4

          Thanks for the help. I ended up just checking the variable inside the function the action triggers so it gets disabled as needed when it's used. It's working perfectly now.

          1 Reply Last reply
          0
          • W wayfarer has marked this topic as solved

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved