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
Qt 6.11 is out! See what's new in the release blog

Value from class to class

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 1.4k 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 sashup

    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
    
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on 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 last edited by sashup
      #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))
      
      jsulmJ 1 Reply Last reply
      0
      • S sashup

        @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))
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on 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
        0
        • jsulmJ jsulm

          @sashup said in Value from class to class:

          somethig like?

          Yes, just try...

          S Offline
          S Offline
          sashup
          wrote on 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

          jsulmJ 1 Reply Last reply
          0
          • S sashup

            @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

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #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
            0
            • jsulmJ jsulm

              @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 last edited by sashup
              #7

              @jsulm Thank you but it doesn't work too

              jsulmJ 1 Reply Last reply
              0
              • S sashup

                @jsulm Thank you but it doesn't work too

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 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
                0
                • jsulmJ jsulm

                  @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 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_()) 
                  
                  
                  jsulmJ 1 Reply Last reply
                  0
                  • S sashup

                    @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_()) 
                    
                    
                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 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
                    0
                    • jsulmJ jsulm

                      @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 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

                      • Login

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