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. Calculations in PyQt5
Forum Updated to NodeBB v4.3 + New Features

Calculations in PyQt5

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 3.2k Views 2 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.
  • C Offline
    C Offline
    CEO.
    wrote on last edited by CEO.
    #1

    Hello,

    I am trying to add values of two qLineEdits but my approach is not working. The inputs for the qLineEdits to be calculated are integer values.

    self.val1 = QLineEdit()
    self.val2 = QLineEdit()
    self.balAfter = QLineEdit()
    
    issBal = int(self.val1.text() ) - int(self.val2.text())
    strIsBal = str(issBal)
    
    recBal = int(self.val1.text() ) + int(self.val2.text())
    
    if self.issueBtn.isChecked():
    print("TransactionType is %s" % (self.issueBtn.iss))
    
    print(strIsBal)
    
    self.balAfter.setText(strIsBal)
    
      else:
    
      print("check your values")
    

    I have tried using issBal =:
    issBal = (self.val1.int() ) - int(self.val2.int()) issrcv = issRcv = int(self.val1.int() ) + int(self.val2.int())

    but it didn't work. Any clue on how to resolve this in PyQt5?

    JonBJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi

      "Didnt work" is a bad description of the result you got.

      it didn't compile or the result was wrong etc ?

      In any case, QString has a toInt() function you can use to get real integers.

      I dont know what
      int(self.val1.text() )
      gives in python as its so typeless
      but in c++ it would be deeply wrong.

      C 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi

        "Didnt work" is a bad description of the result you got.

        it didn't compile or the result was wrong etc ?

        In any case, QString has a toInt() function you can use to get real integers.

        I dont know what
        int(self.val1.text() )
        gives in python as its so typeless
        but in c++ it would be deeply wrong.

        C Offline
        C Offline
        CEO.
        wrote on last edited by
        #3

        @mrjj heyyyy it produced no result when I clicked on its function button. I also tried toInt method but it didn't result to anything when I inputted the values

         ```issBal = int(self.balb4.text().toInt()) - int(self.qttytx.text().toInt())```
        
        1 Reply Last reply
        0
        • C CEO.

          Hello,

          I am trying to add values of two qLineEdits but my approach is not working. The inputs for the qLineEdits to be calculated are integer values.

          self.val1 = QLineEdit()
          self.val2 = QLineEdit()
          self.balAfter = QLineEdit()
          
          issBal = int(self.val1.text() ) - int(self.val2.text())
          strIsBal = str(issBal)
          
          recBal = int(self.val1.text() ) + int(self.val2.text())
          
          if self.issueBtn.isChecked():
          print("TransactionType is %s" % (self.issueBtn.iss))
          
          print(strIsBal)
          
          self.balAfter.setText(strIsBal)
          
            else:
          
            print("check your values")
          

          I have tried using issBal =:
          issBal = (self.val1.int() ) - int(self.val2.int()) issrcv = issRcv = int(self.val1.int() ) + int(self.val2.int())

          but it didn't work. Any clue on how to resolve this in PyQt5?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @CEO
          In addition to @jsulm. If "it's not working" would it not be a good to save your intermediate results like int(self.val1.text() ) into a variable to verify its value is what you expect it to be?

          C 1 Reply Last reply
          0
          • JonBJ JonB

            @CEO
            In addition to @jsulm. If "it's not working" would it not be a good to save your intermediate results like int(self.val1.text() ) into a variable to verify its value is what you expect it to be?

            C Offline
            C Offline
            CEO.
            wrote on last edited by
            #5

            @JonB owing to the fact that I would be setting the text of the result in another qLlineEdit, I am unable to set the result in the qLineEdit without first converting it to string.

            JonBJ 1 Reply Last reply
            0
            • C CEO.

              @JonB owing to the fact that I would be setting the text of the result in another qLlineEdit, I am unable to set the result in the qLineEdit without first converting it to string.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @CEO
              Sorry, I'm not with you. You are (trying to) convert a string from a line edit to an integer. You may know what is in the text edit, but we do not. Since I presume that in order to work your calculation must have the desired input numbers, if it were me I would want to verify and print what your calculation is getting for its numbers. For example if self.val1.text() == "" or self.val1.text() == "unintended" would Python happily return int(self.val1.text() ) as 0?

              Otherwise I cannot see how "trying to add values of two qLineEdits" could not give the intended result.

              C 2 Replies Last reply
              1
              • JonBJ JonB

                @CEO
                Sorry, I'm not with you. You are (trying to) convert a string from a line edit to an integer. You may know what is in the text edit, but we do not. Since I presume that in order to work your calculation must have the desired input numbers, if it were me I would want to verify and print what your calculation is getting for its numbers. For example if self.val1.text() == "" or self.val1.text() == "unintended" would Python happily return int(self.val1.text() ) as 0?

                Otherwise I cannot see how "trying to add values of two qLineEdits" could not give the intended result.

                C Offline
                C Offline
                CEO.
                wrote on last edited by
                #7

                @JonB I stated in the main post that the values for the qLineEdits are of data type INT.
                Now when I tried to use

                
                issBal = int(self.val.text()) + int(self.val2.text())
                                                              self.balAfter.setText(IsBal)
                

                it says "invalid literal for int() with base 10: '' "

                JonBJ 1 Reply Last reply
                0
                • C CEO.

                  @JonB I stated in the main post that the values for the qLineEdits are of data type INT.
                  Now when I tried to use

                  
                  issBal = int(self.val.text()) + int(self.val2.text())
                                                                self.balAfter.setText(IsBal)
                  

                  it says "invalid literal for int() with base 10: '' "

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @CEO said in Calculations in PyQt5:

                  I stated in the main post that the values for the qLineEdits are of data type INT.

                  Values in QLineEdits cannot be "of data type INT". They are of type QString, or str if you prefer in Python. You may (try to) convert them to an integer, but that is another matter.

                  it says "invalid literal for int() with base 10: '' "

                  Which seems to be exactly what I suggested. It is telling you that you are passing " " as the string value to int(). Which is why I suggested that you start by printing out what is in self.val.text() before you try to perform conversions to integer and mathematical operations on it.

                  1 Reply Last reply
                  2
                  • JonBJ JonB

                    @CEO
                    Sorry, I'm not with you. You are (trying to) convert a string from a line edit to an integer. You may know what is in the text edit, but we do not. Since I presume that in order to work your calculation must have the desired input numbers, if it were me I would want to verify and print what your calculation is getting for its numbers. For example if self.val1.text() == "" or self.val1.text() == "unintended" would Python happily return int(self.val1.text() ) as 0?

                    Otherwise I cannot see how "trying to add values of two qLineEdits" could not give the intended result.

                    C Offline
                    C Offline
                    CEO.
                    wrote on last edited by
                    #9

                    @JonB

                    I even created another file just to test the codes freely, but it's still not working. I am trying to do calculations using the qLineEdit values but it's not working. Let's use this as an example:

                            val1 = QLabel(self)
                    
                            val1.setText("val1:")
                    
                            val1.setFont(QFont("Times, BOLD", 10))
                    
                            val1.move(30, 30)
                    
                            val1.resize(130, 30)
                    
                    
                    
                            self.vTx = QLineEdit(self)
                    
                            self.vTx.move(300, 30)
                    
                            self.vTx.resize(350, 30)
                    
                            self.vTx.setFont(QFont("Times, Bold", 10))
                    
                    
                    
                            val2 = QLabel(self)
                    
                            val2.setText("val2:")
                    
                            val2.setFont(QFont("Times, Bold", 10))
                    
                            val2.move(30, 80)
                    
                            val2.resize(200, 30)
                    
                    
                    
                            self.v2Tx = QLineEdit(self)
                    
                            self.v2Tx.move(300, 80)
                    
                            self.v2Tx.resize(350, 30)
                    
                            self.v2Tx.setFont(QFont("Times, Bold", 10))
                    
                    
                    
                    
                    
                            self.v3Tx = QLineEdit(self)
                    
                            self.v3Tx.move(700, 30)
                    
                            self.v3Tx.resize(250, 30)
                    
                            self.v3Tx.setFont(QFont("Times, Bold", 10))
                    
                    
                    
                            btn1 = QPushButton('Register new user', self)
                    
                            btn1.move(100, 350)
                    
                            btn1.resize(150, 30)
                    
                            btn1.clicked.connect(self.calc)
                    
                    
                    
                            self.vall = self.vTx.text()
                    
                            self.vall2 = self.v2Tx.text()
                    
                    
                    
                        def calc(self):
                    
                            print(self.v2Tx.text)
                    
                            try:
                    
                                total = int(self.vall) + int(self.vall2)
                    
                                strT = str(total)
                    
                                print(self.v3Tx.setText(strT))
                    
                            except Exception as err:
                    
                                print(err)
                    
                    JonBJ J.HilkJ 2 Replies Last reply
                    0
                    • C CEO.

                      @JonB

                      I even created another file just to test the codes freely, but it's still not working. I am trying to do calculations using the qLineEdit values but it's not working. Let's use this as an example:

                              val1 = QLabel(self)
                      
                              val1.setText("val1:")
                      
                              val1.setFont(QFont("Times, BOLD", 10))
                      
                              val1.move(30, 30)
                      
                              val1.resize(130, 30)
                      
                      
                      
                              self.vTx = QLineEdit(self)
                      
                              self.vTx.move(300, 30)
                      
                              self.vTx.resize(350, 30)
                      
                              self.vTx.setFont(QFont("Times, Bold", 10))
                      
                      
                      
                              val2 = QLabel(self)
                      
                              val2.setText("val2:")
                      
                              val2.setFont(QFont("Times, Bold", 10))
                      
                              val2.move(30, 80)
                      
                              val2.resize(200, 30)
                      
                      
                      
                              self.v2Tx = QLineEdit(self)
                      
                              self.v2Tx.move(300, 80)
                      
                              self.v2Tx.resize(350, 30)
                      
                              self.v2Tx.setFont(QFont("Times, Bold", 10))
                      
                      
                      
                      
                      
                              self.v3Tx = QLineEdit(self)
                      
                              self.v3Tx.move(700, 30)
                      
                              self.v3Tx.resize(250, 30)
                      
                              self.v3Tx.setFont(QFont("Times, Bold", 10))
                      
                      
                      
                              btn1 = QPushButton('Register new user', self)
                      
                              btn1.move(100, 350)
                      
                              btn1.resize(150, 30)
                      
                              btn1.clicked.connect(self.calc)
                      
                      
                      
                              self.vall = self.vTx.text()
                      
                              self.vall2 = self.v2Tx.text()
                      
                      
                      
                          def calc(self):
                      
                              print(self.v2Tx.text)
                      
                              try:
                      
                                  total = int(self.vall) + int(self.vall2)
                      
                                  strT = str(total)
                      
                                  print(self.v3Tx.setText(strT))
                      
                              except Exception as err:
                      
                                  print(err)
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @CEO said in Calculations in PyQt5:

                      I am trying to do calculations using the qLineEdit values but it's not working.

                      I keep saying: until it's working, stop going int(self.vall) + int(self.vall2) until you have first gone print self.val1 and print self.vall2 on the line above, so we and you know what is actually going on.

                              self.vall = self.vTx.text()
                      
                              self.vall2 = self.v2Tx.text()
                      

                      What do you think is in self.vall or self.vall2 after these lines? Why don't you print them out? Since you have only just created self.vTx and self.v2Tx as new, empty QLineEdits in the code just above, they are going to be empty, aren't they?

                      C 1 Reply Last reply
                      1
                      • C CEO.

                        @JonB

                        I even created another file just to test the codes freely, but it's still not working. I am trying to do calculations using the qLineEdit values but it's not working. Let's use this as an example:

                                val1 = QLabel(self)
                        
                                val1.setText("val1:")
                        
                                val1.setFont(QFont("Times, BOLD", 10))
                        
                                val1.move(30, 30)
                        
                                val1.resize(130, 30)
                        
                        
                        
                                self.vTx = QLineEdit(self)
                        
                                self.vTx.move(300, 30)
                        
                                self.vTx.resize(350, 30)
                        
                                self.vTx.setFont(QFont("Times, Bold", 10))
                        
                        
                        
                                val2 = QLabel(self)
                        
                                val2.setText("val2:")
                        
                                val2.setFont(QFont("Times, Bold", 10))
                        
                                val2.move(30, 80)
                        
                                val2.resize(200, 30)
                        
                        
                        
                                self.v2Tx = QLineEdit(self)
                        
                                self.v2Tx.move(300, 80)
                        
                                self.v2Tx.resize(350, 30)
                        
                                self.v2Tx.setFont(QFont("Times, Bold", 10))
                        
                        
                        
                        
                        
                                self.v3Tx = QLineEdit(self)
                        
                                self.v3Tx.move(700, 30)
                        
                                self.v3Tx.resize(250, 30)
                        
                                self.v3Tx.setFont(QFont("Times, Bold", 10))
                        
                        
                        
                                btn1 = QPushButton('Register new user', self)
                        
                                btn1.move(100, 350)
                        
                                btn1.resize(150, 30)
                        
                                btn1.clicked.connect(self.calc)
                        
                        
                        
                                self.vall = self.vTx.text()
                        
                                self.vall2 = self.v2Tx.text()
                        
                        
                        
                            def calc(self):
                        
                                print(self.v2Tx.text)
                        
                                try:
                        
                                    total = int(self.vall) + int(self.vall2)
                        
                                    strT = str(total)
                        
                                    print(self.v3Tx.setText(strT))
                        
                                except Exception as err:
                        
                                    print(err)
                        
                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #11

                        @CEO
                        vall is not defined in the code sniped you posted, there's vall1 and all 2, both are QLables and contain due to this:

                        self.vall = self.vTx.text()
                        
                                self.vall2 = self.v2Tx.text()
                        

                        an empty string, that stays that way because I fail to see a connection to the LineEdits textChanged signal

                        you're trying to cast this to an int and add the numbers, thats going to fail no matter what


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        C 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @CEO said in Calculations in PyQt5:

                          I am trying to do calculations using the qLineEdit values but it's not working.

                          I keep saying: until it's working, stop going int(self.vall) + int(self.vall2) until you have first gone print self.val1 and print self.vall2 on the line above, so we and you know what is actually going on.

                                  self.vall = self.vTx.text()
                          
                                  self.vall2 = self.v2Tx.text()
                          

                          What do you think is in self.vall or self.vall2 after these lines? Why don't you print them out? Since you have only just created self.vTx and self.v2Tx as new, empty QLineEdits in the code just above, they are going to be empty, aren't they?

                          C Offline
                          C Offline
                          CEO.
                          wrote on last edited by CEO.
                          #12

                          @JonB Hello, I've discovered where the error is from. Thanks a lot. I used your method by first printing the texts of the qLineEdits like: print(vTx.text()) and print(v2Tx.text()) It made me realize in my main file, I was using a wrong qLineEdit, a qLE of another class.

                          Thanks. I have resolved it with your help.
                          I believe everyone who commented was right, I was the one who never used the right QLineEdit. I'm grateful

                          JonBJ 1 Reply Last reply
                          1
                          • C CEO.

                            @JonB Hello, I've discovered where the error is from. Thanks a lot. I used your method by first printing the texts of the qLineEdits like: print(vTx.text()) and print(v2Tx.text()) It made me realize in my main file, I was using a wrong qLineEdit, a qLE of another class.

                            Thanks. I have resolved it with your help.
                            I believe everyone who commented was right, I was the one who never used the right QLineEdit. I'm grateful

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #13

                            @CEO
                            When things are going wrong, or not as you anticipate, it's always a good idea to break problematic complex expressions like, say, total = int(self.vall) + int(self.vall2) into its constituent segments and verify what, say, self.vall, self.vall2 really are. Else you start assuming that, say, addition is going wrong, or int() doesn't know how convert strings. One needs to work from/verify the innermost elements outward.

                            If you choose to use a Python debugger while developing it's even easier to examine these things interactively as required.

                            C 1 Reply Last reply
                            1
                            • JonBJ JonB

                              @CEO
                              When things are going wrong, or not as you anticipate, it's always a good idea to break problematic complex expressions like, say, total = int(self.vall) + int(self.vall2) into its constituent segments and verify what, say, self.vall, self.vall2 really are. Else you start assuming that, say, addition is going wrong, or int() doesn't know how convert strings. One needs to work from/verify the innermost elements outward.

                              If you choose to use a Python debugger while developing it's even easier to examine these things interactively as required.

                              C Offline
                              C Offline
                              CEO.
                              wrote on last edited by
                              #14

                              @JonB I agree with you. I'll try to be using your suggested format subsequently

                              1 Reply Last reply
                              0
                              • J.HilkJ J.Hilk

                                @CEO
                                vall is not defined in the code sniped you posted, there's vall1 and all 2, both are QLables and contain due to this:

                                self.vall = self.vTx.text()
                                
                                        self.vall2 = self.v2Tx.text()
                                

                                an empty string, that stays that way because I fail to see a connection to the LineEdits textChanged signal

                                you're trying to cast this to an int and add the numbers, thats going to fail no matter what

                                C Offline
                                C Offline
                                CEO.
                                wrote on last edited by
                                #15

                                @J-Hilk you're right. I've fixed it. Thanks for your contribution.

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Hi,

                                  Just in case, for "integer only QLineEdit" there's QSpinBox. No need to fiddle with strings at all.

                                  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

                                  • Login

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