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. sum of the click on button
Qt 6.11 is out! See what's new in the release blog

sum of the click on button

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 1.0k Views
  • 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.
  • S Offline
    S Offline
    sashup
    wrote on last edited by
    #1

    How can I count sum of the click on button? For exemple, now I have only 1 1 1 if buttons was clicked 3 times. But I need 1+1+1

    class ClassesPage1(QtWidgets.QWizardPage):
        def __init__(self, *args, **kwargs):
            super(ClassesPage1, self).__init__(*args, **kwargs)
            super(ClassesPage1, self).__init__(*args, **kwargs)
    
            self.setTitle("...")
            self.setSubTitle("...")
    
            self.checkBox_1 = QtWidgets.QCheckBox('...')
            self.checkBox_2 = QtWidgets.QCheckBox('...')
            self.checkBox_3 = QtWidgets.QCheckBox('...')
            self.checkBox_4 = QtWidgets.QCheckBox('...')
    
            self.layout = QtWidgets.QVBoxLayout()
            self.layout.addWidget(self.checkBox_1)
            self.layout.addWidget(self.checkBox_2)
            self.layout.addWidget(self.checkBox_3)
            self.layout.addWidget(self.checkBox_4)
            self.setLayout(self.layout)
    
            self.checkBox_1.stateChanged.connect(self.check)
            self.checkBox_2.stateChanged.connect(self.check)
            self.checkBox_3.stateChanged.connect(self.check)
            self.checkBox_4.stateChanged.connect(self.check)
    
        def check(self, state):
            a = 0
            if state == Qt.Checked:
                a = a + 1
            print(a)
    
        def nextId(self):
            return Wizard.class4
    
    JonBJ 1 Reply Last reply
    0
    • S sashup

      How can I count sum of the click on button? For exemple, now I have only 1 1 1 if buttons was clicked 3 times. But I need 1+1+1

      class ClassesPage1(QtWidgets.QWizardPage):
          def __init__(self, *args, **kwargs):
              super(ClassesPage1, self).__init__(*args, **kwargs)
              super(ClassesPage1, self).__init__(*args, **kwargs)
      
              self.setTitle("...")
              self.setSubTitle("...")
      
              self.checkBox_1 = QtWidgets.QCheckBox('...')
              self.checkBox_2 = QtWidgets.QCheckBox('...')
              self.checkBox_3 = QtWidgets.QCheckBox('...')
              self.checkBox_4 = QtWidgets.QCheckBox('...')
      
              self.layout = QtWidgets.QVBoxLayout()
              self.layout.addWidget(self.checkBox_1)
              self.layout.addWidget(self.checkBox_2)
              self.layout.addWidget(self.checkBox_3)
              self.layout.addWidget(self.checkBox_4)
              self.setLayout(self.layout)
      
              self.checkBox_1.stateChanged.connect(self.check)
              self.checkBox_2.stateChanged.connect(self.check)
              self.checkBox_3.stateChanged.connect(self.check)
              self.checkBox_4.stateChanged.connect(self.check)
      
          def check(self, state):
              a = 0
              if state == Qt.Checked:
                  a = a + 1
              print(a)
      
          def nextId(self):
              return Wizard.class4
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @sashup
      Make the counter a member variable of your class. Not a local variable of the check() method. Then it retains its value across calls to check().

      1 Reply Last reply
      1
      • S Offline
        S Offline
        sashup
        wrote on last edited by
        #3

        I solved it like:

        import sys
        from PyQt5 import QtCore, QtGui, QtWidgets
        
        
        class ClassesPage1(QtWidgets.QWizardPage):
            def __init__(self, *args, **kwargs):
                super(ClassesPage1, self).__init__(*args, **kwargs)
        #        super(ClassesPage1, self).__init__(*args, **kwargs)
        
                self.setTitle("...")
                self.setSubTitle("...")
        
                self.checkBox_1 = QtWidgets.QCheckBox('...')
                self.checkBox_2 = QtWidgets.QCheckBox('...')
                self.checkBox_3 = QtWidgets.QCheckBox('...')
                self.checkBox_4 = QtWidgets.QCheckBox('...')
        
                self.layout = QtWidgets.QVBoxLayout()
                self.layout.addWidget(self.checkBox_1)
                self.layout.addWidget(self.checkBox_2)
                self.layout.addWidget(self.checkBox_3)
                self.layout.addWidget(self.checkBox_4)
                self.setLayout(self.layout)
        
                self.checkBox_1.stateChanged.connect(self.check)
                self.checkBox_2.stateChanged.connect(self.check)
                self.checkBox_3.stateChanged.connect(self.check)
                self.checkBox_4.stateChanged.connect(self.check)
                
                self.listCheckBox = [self.checkBox_1, self.checkBox_2, self.checkBox_3, self.checkBox_4]  
                self.a = 0                                                                                    
        
            def check(self, state):
                self.a = 0                                               
        #        if state == Qt.Checked:
        #            a = a + 1
                for checkBox in self.listCheckBox:                       
                    if checkBox.isChecked():                              
                        self.a += 1                                      
                print(self.a)
        
            def nextId(self):
                return Wizard.class4
                
        
        if __name__ == '__main__':
            app = QtWidgets.QApplication(sys.argv)
            w = ClassesPage1()
            w.show()
            sys.exit(app.exec_())        
        
        
        JonBJ 1 Reply Last reply
        0
        • S sashup

          I solved it like:

          import sys
          from PyQt5 import QtCore, QtGui, QtWidgets
          
          
          class ClassesPage1(QtWidgets.QWizardPage):
              def __init__(self, *args, **kwargs):
                  super(ClassesPage1, self).__init__(*args, **kwargs)
          #        super(ClassesPage1, self).__init__(*args, **kwargs)
          
                  self.setTitle("...")
                  self.setSubTitle("...")
          
                  self.checkBox_1 = QtWidgets.QCheckBox('...')
                  self.checkBox_2 = QtWidgets.QCheckBox('...')
                  self.checkBox_3 = QtWidgets.QCheckBox('...')
                  self.checkBox_4 = QtWidgets.QCheckBox('...')
          
                  self.layout = QtWidgets.QVBoxLayout()
                  self.layout.addWidget(self.checkBox_1)
                  self.layout.addWidget(self.checkBox_2)
                  self.layout.addWidget(self.checkBox_3)
                  self.layout.addWidget(self.checkBox_4)
                  self.setLayout(self.layout)
          
                  self.checkBox_1.stateChanged.connect(self.check)
                  self.checkBox_2.stateChanged.connect(self.check)
                  self.checkBox_3.stateChanged.connect(self.check)
                  self.checkBox_4.stateChanged.connect(self.check)
                  
                  self.listCheckBox = [self.checkBox_1, self.checkBox_2, self.checkBox_3, self.checkBox_4]  
                  self.a = 0                                                                                    
          
              def check(self, state):
                  self.a = 0                                               
          #        if state == Qt.Checked:
          #            a = a + 1
                  for checkBox in self.listCheckBox:                       
                      if checkBox.isChecked():                              
                          self.a += 1                                      
                  print(self.a)
          
              def nextId(self):
                  return Wizard.class4
                  
          
          if __name__ == '__main__':
              app = QtWidgets.QApplication(sys.argv)
              w = ClassesPage1()
              w.show()
              sys.exit(app.exec_())        
          
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @sashup
          Your changed code counts something very different. Now there is no reason to have self.a as a member variable, it can be a local variable.

          S 1 Reply Last reply
          0
          • JonBJ JonB

            @sashup
            Your changed code counts something very different. Now there is no reason to have self.a as a member variable, it can be a local variable.

            S Offline
            S Offline
            sashup
            wrote on last edited by
            #5

            @JonB Thank. I dont understend everything because I started to learn python

            1 Reply Last reply
            0

            • Login

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