PYQT6.4 QTableWidget.sortItems function, Pycharm is throwing a syntax issue even though syntax follows documentation

Question:

Here is the documentation: QTableWidget.sortItems

This is the function:

PySide6.QtWidgets.QTableWidget.sortItems(column[, order=Qt.AscendingOrder])

The following is my code:

self.notes_current_table_widget.sortItems(column[2, order=Qt.AscendingOrder])

PyCharm displays this as an error, when I try to run I get:

SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

Syntax error is in regards to: order=Qt.AscendingOrder

I have tried on other QTableWidgets with the same issue. I have verified that the type is QTableWidget. I have updated all packages to the latest within the project. I have checked syntax of lines before and after.

I ran into this issue because I wanted to sort a dataframe and display the dataframe using a QTableWidget. df = dataframe. The dataframe is sorted until adding the df to the QTableWidget. That’s when I looked at this QTableWidget.sortItems function. Any thoughts or ideas?

Asked By: Andy Krauland

||

Answers:

The docs say:

PySide6.QtWidgets.QTableWidget.sortItems(column[, order=Qt.AscendingOrder])

The square brackets here indicate that order is an optional argument, with default value Qt.AscendingOrder. You’re not supposed to write the square brackets explicitly.

So, your code should probably look like this:

self.notes_current_table_widget.sortItems(column=2, order=Qt.AscendingOrder)

Or leave out order altogether, as you’re using the default value anyway.

Answered By: djvg
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.