Python PyQt - Checkbox to check all other checkboxes












1















My question is very similar to this post, Python PyQt - Checkbox to uncheck all other checkboxes. However, I am trying to check all other boxes when main checkbox is selected and at the same time, if any of the other boxes are selected independently, then I would like to deselect the main checkbox. I tried modifying the answer provided, but not able to put my head around the 'self.sender' signal. I am not able to change the selection when I deselect a checkbox. Here is the code that I modified using the solution provided by @ eyllanesc.Any help is greatly appreciated, thanks!



import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Test(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.checkBoxAll = QCheckBox("Select All")
self.checkBoxA = QCheckBox("Select A")
self.checkBoxB = QCheckBox("Select B")

self.checkBoxAll.setChecked(False)
self.checkBoxAll.stateChanged.connect(self.onStateChange)
self.checkBoxA.stateChanged.connect(self.onStateChange)
self.checkBoxB.stateChanged.connect(self.onStateChange)

grid = QGridLayout(self)

grid.addWidget(self.checkBoxAll, 1, 0)
grid.addWidget(self.checkBoxA, 2, 0)
grid.addWidget(self.checkBoxB, 3, 0)
self.setWindowTitle('Test')
self.show()

@pyqtSlot(int)
def onStateChange(self, state):
if state == Qt.Checked:
if self.sender() == self.checkBoxAll:
self.checkBoxA.setChecked(True)
self.checkBoxB.setChecked(True)
elif self.sender() in (self.checkBoxA, self.checkBoxB):
self.checkBoxAll.setChecked(False)









share|improve this question





























    1















    My question is very similar to this post, Python PyQt - Checkbox to uncheck all other checkboxes. However, I am trying to check all other boxes when main checkbox is selected and at the same time, if any of the other boxes are selected independently, then I would like to deselect the main checkbox. I tried modifying the answer provided, but not able to put my head around the 'self.sender' signal. I am not able to change the selection when I deselect a checkbox. Here is the code that I modified using the solution provided by @ eyllanesc.Any help is greatly appreciated, thanks!



    import sys
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *

    class Test(QWidget):
    def __init__(self):
    super().__init__()
    self.initUI()

    def initUI(self):
    self.checkBoxAll = QCheckBox("Select All")
    self.checkBoxA = QCheckBox("Select A")
    self.checkBoxB = QCheckBox("Select B")

    self.checkBoxAll.setChecked(False)
    self.checkBoxAll.stateChanged.connect(self.onStateChange)
    self.checkBoxA.stateChanged.connect(self.onStateChange)
    self.checkBoxB.stateChanged.connect(self.onStateChange)

    grid = QGridLayout(self)

    grid.addWidget(self.checkBoxAll, 1, 0)
    grid.addWidget(self.checkBoxA, 2, 0)
    grid.addWidget(self.checkBoxB, 3, 0)
    self.setWindowTitle('Test')
    self.show()

    @pyqtSlot(int)
    def onStateChange(self, state):
    if state == Qt.Checked:
    if self.sender() == self.checkBoxAll:
    self.checkBoxA.setChecked(True)
    self.checkBoxB.setChecked(True)
    elif self.sender() in (self.checkBoxA, self.checkBoxB):
    self.checkBoxAll.setChecked(False)









    share|improve this question



























      1












      1








      1








      My question is very similar to this post, Python PyQt - Checkbox to uncheck all other checkboxes. However, I am trying to check all other boxes when main checkbox is selected and at the same time, if any of the other boxes are selected independently, then I would like to deselect the main checkbox. I tried modifying the answer provided, but not able to put my head around the 'self.sender' signal. I am not able to change the selection when I deselect a checkbox. Here is the code that I modified using the solution provided by @ eyllanesc.Any help is greatly appreciated, thanks!



      import sys
      from PyQt5.QtWidgets import *
      from PyQt5.QtCore import *

      class Test(QWidget):
      def __init__(self):
      super().__init__()
      self.initUI()

      def initUI(self):
      self.checkBoxAll = QCheckBox("Select All")
      self.checkBoxA = QCheckBox("Select A")
      self.checkBoxB = QCheckBox("Select B")

      self.checkBoxAll.setChecked(False)
      self.checkBoxAll.stateChanged.connect(self.onStateChange)
      self.checkBoxA.stateChanged.connect(self.onStateChange)
      self.checkBoxB.stateChanged.connect(self.onStateChange)

      grid = QGridLayout(self)

      grid.addWidget(self.checkBoxAll, 1, 0)
      grid.addWidget(self.checkBoxA, 2, 0)
      grid.addWidget(self.checkBoxB, 3, 0)
      self.setWindowTitle('Test')
      self.show()

      @pyqtSlot(int)
      def onStateChange(self, state):
      if state == Qt.Checked:
      if self.sender() == self.checkBoxAll:
      self.checkBoxA.setChecked(True)
      self.checkBoxB.setChecked(True)
      elif self.sender() in (self.checkBoxA, self.checkBoxB):
      self.checkBoxAll.setChecked(False)









      share|improve this question
















      My question is very similar to this post, Python PyQt - Checkbox to uncheck all other checkboxes. However, I am trying to check all other boxes when main checkbox is selected and at the same time, if any of the other boxes are selected independently, then I would like to deselect the main checkbox. I tried modifying the answer provided, but not able to put my head around the 'self.sender' signal. I am not able to change the selection when I deselect a checkbox. Here is the code that I modified using the solution provided by @ eyllanesc.Any help is greatly appreciated, thanks!



      import sys
      from PyQt5.QtWidgets import *
      from PyQt5.QtCore import *

      class Test(QWidget):
      def __init__(self):
      super().__init__()
      self.initUI()

      def initUI(self):
      self.checkBoxAll = QCheckBox("Select All")
      self.checkBoxA = QCheckBox("Select A")
      self.checkBoxB = QCheckBox("Select B")

      self.checkBoxAll.setChecked(False)
      self.checkBoxAll.stateChanged.connect(self.onStateChange)
      self.checkBoxA.stateChanged.connect(self.onStateChange)
      self.checkBoxB.stateChanged.connect(self.onStateChange)

      grid = QGridLayout(self)

      grid.addWidget(self.checkBoxAll, 1, 0)
      grid.addWidget(self.checkBoxA, 2, 0)
      grid.addWidget(self.checkBoxB, 3, 0)
      self.setWindowTitle('Test')
      self.show()

      @pyqtSlot(int)
      def onStateChange(self, state):
      if state == Qt.Checked:
      if self.sender() == self.checkBoxAll:
      self.checkBoxA.setChecked(True)
      self.checkBoxB.setChecked(True)
      elif self.sender() in (self.checkBoxA, self.checkBoxB):
      self.checkBoxAll.setChecked(False)






      python python-3.x pyqt pyqt5 qcheckbox






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 23:19









      eyllanesc

      85.8k103564




      85.8k103564










      asked Jan 2 at 23:18









      pyPNpyPN

      84




      84
























          1 Answer
          1






          active

          oldest

          votes


















          0














          With the logic that you have you are creating a loop since the change of state of any element to change the state of another element, the idea is to block the emission of signals when the change of state is implemented in the slot with blockSignals():



          from PyQt5 import QtCore, QtGui, QtWidgets

          class Test(QtWidgets.QWidget):
          def __init__(self):
          super().__init__()
          self.initUI()

          def initUI(self):
          self.checkBoxAll = QtWidgets.QCheckBox("Select All")
          self.checkBoxAll.setChecked(False)
          self.checkBoxAll.stateChanged.connect(self.onStateChangePrincipal)

          self.checkBoxA = QtWidgets.QCheckBox("Select A")
          self.checkBoxB = QtWidgets.QCheckBox("Select B")
          self.checkboxes = [self.checkBoxA, self.checkBoxB]

          for checkbox in self.checkboxes:
          checkbox.stateChanged.connect(self.onStateChange)

          grid = QtWidgets.QGridLayout(self)
          grid.addWidget(self.checkBoxAll, 1, 0)
          grid.addWidget(self.checkBoxA, 2, 0)
          grid.addWidget(self.checkBoxB, 3, 0)
          self.setWindowTitle('Test')

          @QtCore.pyqtSlot(int)
          def onStateChangePrincipal(self, state):
          if state == QtCore.Qt.Checked:
          for checkbox in self.checkboxes:
          checkbox.blockSignals(True)
          checkbox.setCheckState(state)
          checkbox.blockSignals(False)

          @QtCore.pyqtSlot(int)
          def onStateChange(self, state):
          self.checkBoxAll.blockSignals(True)
          self.checkBoxAll.setChecked(QtCore.Qt.Unchecked)
          self.checkBoxAll.blockSignals(False)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = Test()
          w.show()
          sys.exit(app.exec_())





          share|improve this answer
























          • Thanks @eyllanesc! I was unaware of blockSignals option and it solved the problem.

            – pyPN
            Jan 3 at 1:05












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014436%2fpython-pyqt-checkbox-to-check-all-other-checkboxes%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          With the logic that you have you are creating a loop since the change of state of any element to change the state of another element, the idea is to block the emission of signals when the change of state is implemented in the slot with blockSignals():



          from PyQt5 import QtCore, QtGui, QtWidgets

          class Test(QtWidgets.QWidget):
          def __init__(self):
          super().__init__()
          self.initUI()

          def initUI(self):
          self.checkBoxAll = QtWidgets.QCheckBox("Select All")
          self.checkBoxAll.setChecked(False)
          self.checkBoxAll.stateChanged.connect(self.onStateChangePrincipal)

          self.checkBoxA = QtWidgets.QCheckBox("Select A")
          self.checkBoxB = QtWidgets.QCheckBox("Select B")
          self.checkboxes = [self.checkBoxA, self.checkBoxB]

          for checkbox in self.checkboxes:
          checkbox.stateChanged.connect(self.onStateChange)

          grid = QtWidgets.QGridLayout(self)
          grid.addWidget(self.checkBoxAll, 1, 0)
          grid.addWidget(self.checkBoxA, 2, 0)
          grid.addWidget(self.checkBoxB, 3, 0)
          self.setWindowTitle('Test')

          @QtCore.pyqtSlot(int)
          def onStateChangePrincipal(self, state):
          if state == QtCore.Qt.Checked:
          for checkbox in self.checkboxes:
          checkbox.blockSignals(True)
          checkbox.setCheckState(state)
          checkbox.blockSignals(False)

          @QtCore.pyqtSlot(int)
          def onStateChange(self, state):
          self.checkBoxAll.blockSignals(True)
          self.checkBoxAll.setChecked(QtCore.Qt.Unchecked)
          self.checkBoxAll.blockSignals(False)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = Test()
          w.show()
          sys.exit(app.exec_())





          share|improve this answer
























          • Thanks @eyllanesc! I was unaware of blockSignals option and it solved the problem.

            – pyPN
            Jan 3 at 1:05
















          0














          With the logic that you have you are creating a loop since the change of state of any element to change the state of another element, the idea is to block the emission of signals when the change of state is implemented in the slot with blockSignals():



          from PyQt5 import QtCore, QtGui, QtWidgets

          class Test(QtWidgets.QWidget):
          def __init__(self):
          super().__init__()
          self.initUI()

          def initUI(self):
          self.checkBoxAll = QtWidgets.QCheckBox("Select All")
          self.checkBoxAll.setChecked(False)
          self.checkBoxAll.stateChanged.connect(self.onStateChangePrincipal)

          self.checkBoxA = QtWidgets.QCheckBox("Select A")
          self.checkBoxB = QtWidgets.QCheckBox("Select B")
          self.checkboxes = [self.checkBoxA, self.checkBoxB]

          for checkbox in self.checkboxes:
          checkbox.stateChanged.connect(self.onStateChange)

          grid = QtWidgets.QGridLayout(self)
          grid.addWidget(self.checkBoxAll, 1, 0)
          grid.addWidget(self.checkBoxA, 2, 0)
          grid.addWidget(self.checkBoxB, 3, 0)
          self.setWindowTitle('Test')

          @QtCore.pyqtSlot(int)
          def onStateChangePrincipal(self, state):
          if state == QtCore.Qt.Checked:
          for checkbox in self.checkboxes:
          checkbox.blockSignals(True)
          checkbox.setCheckState(state)
          checkbox.blockSignals(False)

          @QtCore.pyqtSlot(int)
          def onStateChange(self, state):
          self.checkBoxAll.blockSignals(True)
          self.checkBoxAll.setChecked(QtCore.Qt.Unchecked)
          self.checkBoxAll.blockSignals(False)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = Test()
          w.show()
          sys.exit(app.exec_())





          share|improve this answer
























          • Thanks @eyllanesc! I was unaware of blockSignals option and it solved the problem.

            – pyPN
            Jan 3 at 1:05














          0












          0








          0







          With the logic that you have you are creating a loop since the change of state of any element to change the state of another element, the idea is to block the emission of signals when the change of state is implemented in the slot with blockSignals():



          from PyQt5 import QtCore, QtGui, QtWidgets

          class Test(QtWidgets.QWidget):
          def __init__(self):
          super().__init__()
          self.initUI()

          def initUI(self):
          self.checkBoxAll = QtWidgets.QCheckBox("Select All")
          self.checkBoxAll.setChecked(False)
          self.checkBoxAll.stateChanged.connect(self.onStateChangePrincipal)

          self.checkBoxA = QtWidgets.QCheckBox("Select A")
          self.checkBoxB = QtWidgets.QCheckBox("Select B")
          self.checkboxes = [self.checkBoxA, self.checkBoxB]

          for checkbox in self.checkboxes:
          checkbox.stateChanged.connect(self.onStateChange)

          grid = QtWidgets.QGridLayout(self)
          grid.addWidget(self.checkBoxAll, 1, 0)
          grid.addWidget(self.checkBoxA, 2, 0)
          grid.addWidget(self.checkBoxB, 3, 0)
          self.setWindowTitle('Test')

          @QtCore.pyqtSlot(int)
          def onStateChangePrincipal(self, state):
          if state == QtCore.Qt.Checked:
          for checkbox in self.checkboxes:
          checkbox.blockSignals(True)
          checkbox.setCheckState(state)
          checkbox.blockSignals(False)

          @QtCore.pyqtSlot(int)
          def onStateChange(self, state):
          self.checkBoxAll.blockSignals(True)
          self.checkBoxAll.setChecked(QtCore.Qt.Unchecked)
          self.checkBoxAll.blockSignals(False)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = Test()
          w.show()
          sys.exit(app.exec_())





          share|improve this answer













          With the logic that you have you are creating a loop since the change of state of any element to change the state of another element, the idea is to block the emission of signals when the change of state is implemented in the slot with blockSignals():



          from PyQt5 import QtCore, QtGui, QtWidgets

          class Test(QtWidgets.QWidget):
          def __init__(self):
          super().__init__()
          self.initUI()

          def initUI(self):
          self.checkBoxAll = QtWidgets.QCheckBox("Select All")
          self.checkBoxAll.setChecked(False)
          self.checkBoxAll.stateChanged.connect(self.onStateChangePrincipal)

          self.checkBoxA = QtWidgets.QCheckBox("Select A")
          self.checkBoxB = QtWidgets.QCheckBox("Select B")
          self.checkboxes = [self.checkBoxA, self.checkBoxB]

          for checkbox in self.checkboxes:
          checkbox.stateChanged.connect(self.onStateChange)

          grid = QtWidgets.QGridLayout(self)
          grid.addWidget(self.checkBoxAll, 1, 0)
          grid.addWidget(self.checkBoxA, 2, 0)
          grid.addWidget(self.checkBoxB, 3, 0)
          self.setWindowTitle('Test')

          @QtCore.pyqtSlot(int)
          def onStateChangePrincipal(self, state):
          if state == QtCore.Qt.Checked:
          for checkbox in self.checkboxes:
          checkbox.blockSignals(True)
          checkbox.setCheckState(state)
          checkbox.blockSignals(False)

          @QtCore.pyqtSlot(int)
          def onStateChange(self, state):
          self.checkBoxAll.blockSignals(True)
          self.checkBoxAll.setChecked(QtCore.Qt.Unchecked)
          self.checkBoxAll.blockSignals(False)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = Test()
          w.show()
          sys.exit(app.exec_())






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 23:30









          eyllanesceyllanesc

          85.8k103564




          85.8k103564













          • Thanks @eyllanesc! I was unaware of blockSignals option and it solved the problem.

            – pyPN
            Jan 3 at 1:05



















          • Thanks @eyllanesc! I was unaware of blockSignals option and it solved the problem.

            – pyPN
            Jan 3 at 1:05

















          Thanks @eyllanesc! I was unaware of blockSignals option and it solved the problem.

          – pyPN
          Jan 3 at 1:05





          Thanks @eyllanesc! I was unaware of blockSignals option and it solved the problem.

          – pyPN
          Jan 3 at 1:05




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014436%2fpython-pyqt-checkbox-to-check-all-other-checkboxes%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

          Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

          A Topological Invariant for $pi_3(U(n))$