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



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


  • Lifetime Qt Champion

    Hi,

    What do you have exactly in that cell ?



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



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


  • Moderators

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



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



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


  • Moderators

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



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


  • Moderators

    @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('%'))
    


  • @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" ... ??



  • 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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.