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

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.4k 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 Offline
    P Offline
    Piyush
    wrote on 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))))

    jsulmJ VRoninV 2 Replies Last reply
    0
    • P 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.

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

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

      JonBJ 1 Reply Last reply
      1
      • P Offline
        P Offline
        Piyush
        wrote on 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'>)

        jsulmJ 1 Reply Last reply
        0
        • jsulmJ jsulm

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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

            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'>)

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

              jsulmJ JonBJ 2 Replies Last reply
              0
              • P Piyush

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

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

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 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

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

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on 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
                    • jsulmJ jsulm

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

                      • Login

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