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. CheckBox in qactions not working
Forum Updated to NodeBB v4.3 + New Features

CheckBox in qactions not working

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 2 Posters 700 Views 1 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.
  • Q Offline
    Q Offline
    qt_is_not_easy
    wrote on 21 Mar 2023, 19:06 last edited by
    #1

    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())
    
    1 Reply Last reply
    0
    • S SGaist moved this topic from General and Desktop on 21 Mar 2023, 19:47
    • Q qt_is_not_easy
      22 Mar 2023, 20:10

      @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())
      
      S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 22 Mar 2023, 20:15 last edited by
      #4

      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.

      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
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 21 Mar 2023, 19:50 last edited by
        #2

        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.

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

        Q 1 Reply Last reply 22 Mar 2023, 20:10
        2
        • S SGaist
          21 Mar 2023, 19:50

          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.

          Q Offline
          Q Offline
          qt_is_not_easy
          wrote on 22 Mar 2023, 20:10 last edited by qt_is_not_easy
          #3

          @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())
          
          S 1 Reply Last reply 22 Mar 2023, 20:15
          0
          • Q qt_is_not_easy
            22 Mar 2023, 20:10

            @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())
            
            S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 22 Mar 2023, 20:15 last edited by
            #4

            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.

            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
            1
            • Q qt_is_not_easy has marked this topic as solved on 22 Mar 2023, 21:46

            1/4

            21 Mar 2023, 19:06

            • Login

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