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. While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?
Forum Update on Tuesday, May 27th 2025

While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 6.3k Views 1 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.
  • P Piyush
    24 Jan 2018, 12:28

    While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?

    I tried converting as follows:

    cell = self.ui.widget1.item(r,c).text()
    x=int(cell)

    J Online
    J Online
    JonB
    wrote on 24 Jan 2018, 13:29 last edited by JonB
    #3

    @Piyush
    You are using Python/PyQt. All strs in Python are Unicode. And int() should work fine on them. So what is the value of cell, if it's not an number parsable as an integer int() will barf.

    1 Reply Last reply
    1
    • P Offline
      P Offline
      Piyush
      wrote on 25 Jan 2018, 05:23 last edited by
      #4

      I tried using int(cell), but this does not seems to work. Python does not show any error and does not convert the variable 'cell' into integer. Check type for the variable and still shows unicode.

      Actual Scenario:
      I have used the following method to populate the variable in QTableWidget and want to have a red colour font for negative numbers:

      self.ui.table_widget.setItem(1,1, QTableWidgetItem(str("{:.2%}".format(VariableA - VariableB))))

      Also, I tried removing 'str' from the item input, while setting the value, but even that does not seem to help:
      self.ui.table_widget.setItem(1,1, QTableWidgetItem(("{:.2%}".format(VariableA - VariableB))))

      J V 2 Replies Last reply 25 Jan 2018, 07:41
      0
      • P Piyush
        25 Jan 2018, 05:23

        I tried using int(cell), but this does not seems to work. Python does not show any error and does not convert the variable 'cell' into integer. Check type for the variable and still shows unicode.

        Actual Scenario:
        I have used the following method to populate the variable in QTableWidget and want to have a red colour font for negative numbers:

        self.ui.table_widget.setItem(1,1, QTableWidgetItem(str("{:.2%}".format(VariableA - VariableB))))

        Also, I tried removing 'str' from the item input, while setting the value, but even that does not seem to help:
        self.ui.table_widget.setItem(1,1, QTableWidgetItem(("{:.2%}".format(VariableA - VariableB))))

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 25 Jan 2018, 07:41 last edited by
        #5

        @Piyush said in While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?:

        I tried using int(cell), but this does not seems to work.

        That's why @SGaist asked what is inside cell?
        You can check this with:

        print(type(cell))
        

        cell probably contains an instance of a Qt class and not just a string.

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

        J 1 Reply Last reply 25 Jan 2018, 08:18
        1
        • P Offline
          P Offline
          Piyush
          wrote on 25 Jan 2018, 08:16 last edited by Piyush
          #6

          Cell content varies from text or number. I am retrieving it using the following:

          cell = self.ui.table_widget.item(row,column).text()

          Cell values for ex:

          Ex 1: print cell (output: -4.07%)
          print type(cell) (output: <type 'unicode'>)

          Ex 2: print cell (output: Truncated)
          print type(cell) (output: <type 'unicode'>)

          J 1 Reply Last reply 25 Jan 2018, 08:25
          0
          • J jsulm
            25 Jan 2018, 07:41

            @Piyush said in While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?:

            I tried using int(cell), but this does not seems to work.

            That's why @SGaist asked what is inside cell?
            You can check this with:

            print(type(cell))
            

            cell probably contains an instance of a Qt class and not just a string.

            J Online
            J Online
            JonB
            wrote on 25 Jan 2018, 08:18 last edited by JonB
            #7

            @jsulm

            The OP stated (assuming that's the case) that the code is:
            cell = self.ui.widget1.item(r,c).text()
            So from PyQt that is type str, and in Python that is Unicode.

            @Piyush

            I tried using int(cell), but this does not seems to work. Python does not show any error and does not convert the variable 'cell' into integer. Check type for the variable and still shows unicode.

            What does "does not seems to work" mean? Line:
            x=int(cell)
            doesn't change/convert variable cell, it returns the integer value in x, or raises an error. So after that line what is value of x? And what is value (not type) of cell?

            1 Reply Last reply
            0
            • P Piyush
              25 Jan 2018, 08:16

              Cell content varies from text or number. I am retrieving it using the following:

              cell = self.ui.table_widget.item(row,column).text()

              Cell values for ex:

              Ex 1: print cell (output: -4.07%)
              print type(cell) (output: <type 'unicode'>)

              Ex 2: print cell (output: Truncated)
              print type(cell) (output: <type 'unicode'>)

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 25 Jan 2018, 08:25 last edited by
              #8

              @Piyush You cannot convert strings like "-4.07%" to a number using int() because it contains % character ans isn't an integer at all. Furthermore int() would convert to integer - do you want integer or floating point numbers?

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

              1 Reply Last reply
              1
              • P Offline
                P Offline
                Piyush
                wrote on 25 Jan 2018, 08:38 last edited by
                #9

                @Piyush You cannot convert strings like "-4.07%" to a number using int() because it contains % character ans isn't an integer at all. Furthermore int() would convert to integer - do you want integer or floating point numbers?

                @jsulm I want a floating point number. Do i need to change the method to retrieve cell value?

                What does "does not seems to work" mean? Line:
                x=int(cell)
                doesn't change/convert variable cell, it returns the integer value in x, or raises an error. So after that line what is value of x? And what is value (not type) of cell?

                @JonB x=int(cell) does not change/convert variable cell. I tried printing the value of x, but there was nothing in x, which means the conversion is not working. @jsulm is right as % string value cannot be converted into floating point number.

                J J 2 Replies Last reply 25 Jan 2018, 08:44
                0
                • P Piyush
                  25 Jan 2018, 08:38

                  @Piyush You cannot convert strings like "-4.07%" to a number using int() because it contains % character ans isn't an integer at all. Furthermore int() would convert to integer - do you want integer or floating point numbers?

                  @jsulm I want a floating point number. Do i need to change the method to retrieve cell value?

                  What does "does not seems to work" mean? Line:
                  x=int(cell)
                  doesn't change/convert variable cell, it returns the integer value in x, or raises an error. So after that line what is value of x? And what is value (not type) of cell?

                  @JonB x=int(cell) does not change/convert variable cell. I tried printing the value of x, but there was nothing in x, which means the conversion is not working. @jsulm is right as % string value cannot be converted into floating point number.

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 25 Jan 2018, 08:44 last edited by
                  #10

                  @Piyush said in While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?:

                  I want a floating point number

                  Then why do you use int() instead of float()?
                  Use float:

                  float(cell.strip('%'))
                  

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

                  P 1 Reply Last reply 25 Jan 2018, 10:11
                  2
                  • P Piyush
                    25 Jan 2018, 08:38

                    @Piyush You cannot convert strings like "-4.07%" to a number using int() because it contains % character ans isn't an integer at all. Furthermore int() would convert to integer - do you want integer or floating point numbers?

                    @jsulm I want a floating point number. Do i need to change the method to retrieve cell value?

                    What does "does not seems to work" mean? Line:
                    x=int(cell)
                    doesn't change/convert variable cell, it returns the integer value in x, or raises an error. So after that line what is value of x? And what is value (not type) of cell?

                    @JonB x=int(cell) does not change/convert variable cell. I tried printing the value of x, but there was nothing in x, which means the conversion is not working. @jsulm is right as % string value cannot be converted into floating point number.

                    J Online
                    J Online
                    JonB
                    wrote on 25 Jan 2018, 08:47 last edited by JonB
                    #11

                    @Piyush said in While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?:

                    x=int(cell)
                    doesn't change/convert variable cell, it returns the integer value in x, or raises an error. So after that line what is value of x? And what is value (not type) of cell?

                    @JonB x=int(cell) does not change/convert variable cell. I tried printing the value of x, but there was nothing in x, which means the conversion is not working. @jsulm is right as % string value cannot be converted into floating point number.

                    Here is Python (3) output for what you are saying is the case:

                    >>> int("-4.07%")
                    Traceback (most recent call last):
                      File "<stdin>", line 1, in <module>
                    ValueError: invalid literal for int() with base 10: '-4.07%'
                    >>> 
                    

                    Meanwhile, you say from your code " Python does not show any error" ... ??

                    1 Reply Last reply
                    0
                    • P Piyush
                      25 Jan 2018, 05:23

                      I tried using int(cell), but this does not seems to work. Python does not show any error and does not convert the variable 'cell' into integer. Check type for the variable and still shows unicode.

                      Actual Scenario:
                      I have used the following method to populate the variable in QTableWidget and want to have a red colour font for negative numbers:

                      self.ui.table_widget.setItem(1,1, QTableWidgetItem(str("{:.2%}".format(VariableA - VariableB))))

                      Also, I tried removing 'str' from the item input, while setting the value, but even that does not seem to help:
                      self.ui.table_widget.setItem(1,1, QTableWidgetItem(("{:.2%}".format(VariableA - VariableB))))

                      V Offline
                      V Offline
                      VRonin
                      wrote on 25 Jan 2018, 08:48 last edited by
                      #12

                      The problem is here:

                      self.ui.table_widget.setItem(1,1, QTableWidgetItem(str("{:.2%}".format(VariableA - VariableB))))

                      QTableWidgetItem's constructor is confusing, see https://bugreports.qt.io/browse/QTBUG-65555

                      use:

                      newItem = QTableWidgetItem();
                      newItem-.setData(Qt::EditRole,VariableA - VariableB);
                      self.ui.table_widget.setItem(1,1, newItem);
                      

                      to format the output as a 2 decimals percentage subclass QStyledItemDelegate, reiplemet displayText to use that format and apply it to the column you want with self.ui.table_widget.setItemDelegateForColumn

                      P.S.
                      As a general rule, remember that the model should never handle how the data is rendered, that's the job of the delegate

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      1
                      • J jsulm
                        25 Jan 2018, 08:44

                        @Piyush said in While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?:

                        I want a floating point number

                        Then why do you use int() instead of float()?
                        Use float:

                        float(cell.strip('%'))
                        
                        P Offline
                        P Offline
                        Piyush
                        wrote on 25 Jan 2018, 10:11 last edited by
                        #13

                        @Piyush said in While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?:

                        I want a floating point number

                        Then why do you use int() instead of float()?
                        Use float:

                        float(cell.strip('%'))
                        

                        Thanks @jsulm. This solved the issue.

                        1 Reply Last reply
                        0

                        12/13

                        25 Jan 2018, 08:48

                        • Login

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