pythonpyqt5qtablewidgetqtablewidgetitem

Can't add two columns in QTableWidget. Error - DeprecationWarning: an integer is required (got type float)


I am attempting to create a spreadsheet with QTableWidget from PyQt5. I want to add the values of two columns together and display the results in a third. But I keep getting the error DeprecationWarning: an integer is required (got type float). Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.

This is the function I wrote to add the columns.

def add_columns(self):
        new_cols = self.tableWidget.columnCount()
        for self.row in range (self.tableWidget.rowCount()):
            add_2_cols = float((self.tableWidget.item(self.row, 0)).text()) + float((self.tableWidget.item(self.row, 1)).text())
            add_2_cols = QTableWidgetItem(add_2_cols)
            self.tableWidget.setItem(self.row, 3, add_2_cols)

Alternatively, it would be nice if there was a preset function to do this but I couldn't find one.


Solution

  • That is because you're trying to create the QTableWidgetItem using the result of the sum (which is a float) as first argument.

    One of the constructors of QTableWidgetItem accepts an integer.
    From the "Subclassing" section of the docs

    When subclassing QTableWidgetItem to provide custom items, it is possible to define new types for them so that they can be distinguished from standard items. The constructors for subclasses that require this feature need to call the base class constructor with a new type value equal to or greater than UserType

    So, not only you're providing an invalid type (since it should be an integer), but you're using it for the wrong reason.

    If you want to create an item with the result as its text, you must convert it into a string:

    add_2_cols = QTableWidgetItem(str(add_2_cols))
    

    Alternatively, you can create an "empty" item and use setData() to set its display value while storing it in numerical form:

    add_2_cols = QTableWidgetItem()
    add_2_cols.setData(QtCore.Qt.DisplayRole, add_2_cols)
    

    This will still let text() work with that item, but with the benefit that you can have the correct numerical value using data() without risking any conversion:

    item = self.tableWidget.item(row, column)
    if item:
        value = item.data(QtCore.Qt.DisplayRole)
    

    On an unrelated note, consider that setting an instance attribute (like you did with self.row) in a for cycle is normally not suggested: first of all, if not used properly, it could lead to unexpected behavior, bugs and crashes; but also, since every iteration overwrites the previous reference, it makes completely pointless to have a "static" reference at all.