Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Value from class to class
Forum Updated to NodeBB v4.3 + New Features

Value from class to class

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 851 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 10 May 2021, 10:51 last edited by sashup 5 Oct 2021, 10:55
    #1

    How to pass value mean from class to class? I try to do it with the help of class parametrs but It doesn't work.

    class ClassesPage1(QtWidgets.QWizardPage):
        ...
        def check(self):
            a = 0
            for checkBox in self.listCheckBox:
                if checkBox.isChecked():
                    a += 1
            mean = a / 4
    
            if mean == 1:
                self.label_1.setText('Avarage: 1')
            elif mean == 0:
                self.label_1.setText('Avarage: 0')
            else:
                self.label_1.setText('Avarage:  ' + str(mean))
    
        def nextId(self):
            return Wizard.class4
    
    class parametrs:
        par = ClassesPage1()
        print par.mean
    
    
    J 1 Reply Last reply 10 May 2021, 11:11
    0
    • S sashup
      10 May 2021, 10:51

      How to pass value mean from class to class? I try to do it with the help of class parametrs but It doesn't work.

      class ClassesPage1(QtWidgets.QWizardPage):
          ...
          def check(self):
              a = 0
              for checkBox in self.listCheckBox:
                  if checkBox.isChecked():
                      a += 1
              mean = a / 4
      
              if mean == 1:
                  self.label_1.setText('Avarage: 1')
              elif mean == 0:
                  self.label_1.setText('Avarage: 0')
              else:
                  self.label_1.setText('Avarage:  ' + str(mean))
      
          def nextId(self):
              return Wizard.class4
      
      class parametrs:
          par = ClassesPage1()
          print par.mean
      
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 10 May 2021, 11:11 last edited by
      #2

      @sashup "mean" is local variable and only exists inside def check(self). It needs to be class member (self.mean) if you want to use it outside of def check(self).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sashup
        wrote on 10 May 2021, 11:49 last edited by sashup 5 Oct 2021, 11:50
        #3

        @jsulm
        somethig like?

            def check(self):
                a = 0
                for checkBox in self.listCheckBox:
                    if checkBox.isChecked():
                        a += 1
                self.mean = a / 4
        
                if self.mean == 1:
                    self.label_1.setText('Avarage: 1')
                elif self.mean == 0:
                    self.label_1.setText('Avarage: 0')
                else:
                    self.label_1.setText('Avarage:' + str(self.mean))
        
        J 1 Reply Last reply 10 May 2021, 11:54
        0
        • S sashup
          10 May 2021, 11:49

          @jsulm
          somethig like?

              def check(self):
                  a = 0
                  for checkBox in self.listCheckBox:
                      if checkBox.isChecked():
                          a += 1
                  self.mean = a / 4
          
                  if self.mean == 1:
                      self.label_1.setText('Avarage: 1')
                  elif self.mean == 0:
                      self.label_1.setText('Avarage: 0')
                  else:
                      self.label_1.setText('Avarage:' + str(self.mean))
          
          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 10 May 2021, 11:54 last edited by
          #4

          @sashup said in Value from class to class:

          somethig like?

          Yes, just try...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply 10 May 2021, 11:56
          0
          • J jsulm
            10 May 2021, 11:54

            @sashup said in Value from class to class:

            somethig like?

            Yes, just try...

            S Offline
            S Offline
            sashup
            wrote on 10 May 2021, 11:56 last edited by
            #5

            @jsulm
            Then in the second class I write:

            class parametrs:
                def __init__(self, *args, **kwargs):
                    super(parametrs, self).__init__(*args, **kwargs)
                    print(self.mean)
            
            

            But It doesn't work

            J 1 Reply Last reply 10 May 2021, 12:00
            0
            • S sashup
              10 May 2021, 11:56

              @jsulm
              Then in the second class I write:

              class parametrs:
                  def __init__(self, *args, **kwargs):
                      super(parametrs, self).__init__(*args, **kwargs)
                      print(self.mean)
              
              

              But It doesn't work

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 10 May 2021, 12:00 last edited by jsulm 5 Oct 2021, 12:01
              #6

              @sashup said in Value from class to class:

              But It doesn't work

              Of course not. This code does not make sense.
              "mean" is member of the class ClassesPage1, right? So, how should self.mean work in a completely different class?
              Why did you change parametrs class? Before it was

              class parametrs:
                  par = ClassesPage1()
                  print par.mean
              

              and this version should work now.

              Also, keep in mind that currently self.mean only exists if check() was executed!

              You should really read a book about Python as you're asking basic questions completely unrelated to Qt.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply 10 May 2021, 12:03
              0
              • J jsulm
                10 May 2021, 12:00

                @sashup said in Value from class to class:

                But It doesn't work

                Of course not. This code does not make sense.
                "mean" is member of the class ClassesPage1, right? So, how should self.mean work in a completely different class?
                Why did you change parametrs class? Before it was

                class parametrs:
                    par = ClassesPage1()
                    print par.mean
                

                and this version should work now.

                Also, keep in mind that currently self.mean only exists if check() was executed!

                You should really read a book about Python as you're asking basic questions completely unrelated to Qt.

                S Offline
                S Offline
                sashup
                wrote on 10 May 2021, 12:03 last edited by sashup 5 Oct 2021, 12:08
                #7

                @jsulm Thank you but it doesn't work too

                J 1 Reply Last reply 10 May 2021, 12:03
                0
                • S sashup
                  10 May 2021, 12:03

                  @jsulm Thank you but it doesn't work too

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 10 May 2021, 12:03 last edited by
                  #8

                  @sashup Then please show your current code and write what exactly happens instead of writing that it "doesn't work"...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  S 1 Reply Last reply 10 May 2021, 12:08
                  0
                  • J jsulm
                    10 May 2021, 12:03

                    @sashup Then please show your current code and write what exactly happens instead of writing that it "doesn't work"...

                    S Offline
                    S Offline
                    sashup
                    wrote on 10 May 2021, 12:08 last edited by
                    #9

                    @jsulm print par.mean doesn't give any values

                    class ClassesPage1(QtWidgets.QWizardPage):
                        def __init__(self, *args, **kwargs):
                            super(ClassesPage1, self).__init__(*args, **kwargs)
                    
                            font = QtGui.QFont()
                            font.setFamily("Sitka")
                            font.setPointSize(14)
                            self.setFont(font)
                    
                            self.label_3 = QLabel("...", self)
                            self.label_3.setAlignment(Qt.AlignCenter)
                    
                            self.label_4 = QLabel("...", self)
                            self.label_4.setAlignment(Qt.AlignBottom)
                    
                            self.checkBox_1 = QtWidgets.QCheckBox('...')
                            self.checkBox_2 = QtWidgets.QCheckBox('...')
                            self.checkBox_3 = QtWidgets.QCheckBox('...')
                            self.checkBox_4 = QtWidgets.QCheckBox('...')
                            self.label_1 = QtWidgets.QLabel('Avarage: 0')
                            #self.label_1.setAlignment(Qt.AlignLeft)
                    
                            self.layout = QtWidgets.QVBoxLayout()
                            self.layout.addWidget(self.label_3)
                            self.layout.addWidget(self.label_4)
                            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.layout.addWidget(self.label_1)
                            self.setLayout(self.layout)
                    
                            self.performance()
                            self.listCheckBox = [self.checkBox_1, self.checkBox_2, self.checkBox_3, self.checkBox_4]
                    
                        def performance(self):
                            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):
                        
                                a = 0
                        
                                for checkBox in self.listCheckBox:
                        
                                    if checkBox.isChecked():
                        
                                        a += 1
                        
                                self.mean = a / 4
                        
                        
                        
                                if self.mean == 1:
                        
                                    self.label_1.setText('Avarage: 1')
                        
                                elif self.mean == 0:
                        
                                    self.label_1.setText('Avarage: 0')
                        
                                else:
                        
                                    self.label_1.setText('Avarage:' + str(self.mean))xtId(self):
                            return Wizard.class4
                    
                    class parametrs:
                        par = ClassesPage1()
                        print par.mean
                    
                    if __name__ == '__main__':
                        app = QtWidgets.QApplication(sys.argv)
                        w = ClassesPage1()
                        w.show()
                        sys.exit(app.exec_()) 
                    
                    
                    J 1 Reply Last reply 10 May 2021, 12:11
                    0
                    • S sashup
                      10 May 2021, 12:08

                      @jsulm print par.mean doesn't give any values

                      class ClassesPage1(QtWidgets.QWizardPage):
                          def __init__(self, *args, **kwargs):
                              super(ClassesPage1, self).__init__(*args, **kwargs)
                      
                              font = QtGui.QFont()
                              font.setFamily("Sitka")
                              font.setPointSize(14)
                              self.setFont(font)
                      
                              self.label_3 = QLabel("...", self)
                              self.label_3.setAlignment(Qt.AlignCenter)
                      
                              self.label_4 = QLabel("...", self)
                              self.label_4.setAlignment(Qt.AlignBottom)
                      
                              self.checkBox_1 = QtWidgets.QCheckBox('...')
                              self.checkBox_2 = QtWidgets.QCheckBox('...')
                              self.checkBox_3 = QtWidgets.QCheckBox('...')
                              self.checkBox_4 = QtWidgets.QCheckBox('...')
                              self.label_1 = QtWidgets.QLabel('Avarage: 0')
                              #self.label_1.setAlignment(Qt.AlignLeft)
                      
                              self.layout = QtWidgets.QVBoxLayout()
                              self.layout.addWidget(self.label_3)
                              self.layout.addWidget(self.label_4)
                              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.layout.addWidget(self.label_1)
                              self.setLayout(self.layout)
                      
                              self.performance()
                              self.listCheckBox = [self.checkBox_1, self.checkBox_2, self.checkBox_3, self.checkBox_4]
                      
                          def performance(self):
                              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):
                          
                                  a = 0
                          
                                  for checkBox in self.listCheckBox:
                          
                                      if checkBox.isChecked():
                          
                                          a += 1
                          
                                  self.mean = a / 4
                          
                          
                          
                                  if self.mean == 1:
                          
                                      self.label_1.setText('Avarage: 1')
                          
                                  elif self.mean == 0:
                          
                                      self.label_1.setText('Avarage: 0')
                          
                                  else:
                          
                                      self.label_1.setText('Avarage:' + str(self.mean))xtId(self):
                              return Wizard.class4
                      
                      class parametrs:
                          par = ClassesPage1()
                          print par.mean
                      
                      if __name__ == '__main__':
                          app = QtWidgets.QApplication(sys.argv)
                          w = ClassesPage1()
                          w.show()
                          sys.exit(app.exec_()) 
                      
                      
                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 10 May 2021, 12:11 last edited by
                      #10

                      @sashup I already wrote that self.mean is only set if def check(self) was executed at least once. Do you realise that?
                      This should work:

                      class parametrs:
                          par = ClassesPage1()
                          par.check()
                          print par.mean
                      

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      S 1 Reply Last reply 10 May 2021, 12:42
                      0
                      • J jsulm
                        10 May 2021, 12:11

                        @sashup I already wrote that self.mean is only set if def check(self) was executed at least once. Do you realise that?
                        This should work:

                        class parametrs:
                            par = ClassesPage1()
                            par.check()
                            print par.mean
                        
                        S Offline
                        S Offline
                        sashup
                        wrote on 10 May 2021, 12:42 last edited by
                        #11

                        @jsulm It was solved like

                        import sys
                        from PyQt5 import QtCore, QtGui, QtWidgets
                        
                        
                        class ClassesPage1(QtWidgets.QWizardPage):
                            def __init__(self, parent=None):                                         
                                super(ClassesPage1, self).__init__()
                                self.setTitle("...")
                                self.setSubTitle("...")
                        
                                self.label_1 = QtWidgets.QLabel('label_1')
                        
                                self.checkBox_1 = QtWidgets.QCheckBox('cb1 ...')
                                self.checkBox_2 = QtWidgets.QCheckBox('cb2 ...')
                                self.checkBox_3 = QtWidgets.QCheckBox('cb3 ...')
                                self.checkBox_4 = QtWidgets.QCheckBox('cb4 ...')
                        
                                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.layout.addWidget(self.label_1)
                                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  
                                
                                self.parent = parent                                                 
                                self.mean = 0                                                         
                                
                            
                            def check(self):
                                self.a = 0                                                             
                                for checkBox in self.listCheckBox:
                                    if checkBox.isChecked():
                                        self.a += 1                                                    
                                self.mean = self.a / 4                                                
                                     
                                self.label_1.setText(f'Avarage: {self.mean}')                 
                                self.parent._print()                                                  
                        
                            def nextId(self):
                                return Wizard.class4
                        
                        
                        class Parametrs:
                            def __init__(self):
                                super().__init__()
                                
                                self.par = ClassesPage1(self)
                                self.par.show()
                                print(f'__init__: par.mean = {self.par.mean}')
                            
                            def _print(self):
                                print(f'par.mean = {self.par.mean:.2f}')
                             
                            
                        if __name__ == '__main__':
                            app = QtWidgets.QApplication(sys.argv)
                            w = Parametrs()
                            sys.exit(app.exec_()) 
                        
                        
                        1 Reply Last reply
                        0

                        1/11

                        10 May 2021, 10:51

                        • Login

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