PyQT6 QtableWidget column sorting by properties of CellWidget
03:22 20 Mar 2026

In a QTableWidget, you can add text to a table cell with:

table = QTableWidget()
qti = QTableWidgetItem("some string")
table.setItem(y, x, qti)

or you can add a widget to it with:

qpb = QPushButton("some value or text")
qpb.setProperty("sort-value", 34)
table.setCellWidget(y, x, qpb)
# or
qcb = QComboBox()
qcb.setProperty("sort-value", 2)
table.setCellWidget(y, x, qcb)
# or some other widget

When sorting on columns is enabled, by default it sorts on the visible string in the QTableWidgetItem. Not by values of the CellWidgets.

Columns containing CellWidgets do not sort.

What I need is a way to sort some columns by there default string value (as given in the QTableWidgetItem) and other columns by there sort-value as given in a property or so of the QPushButton or QComboBox or some other widget.

My first thought is: is there a sorting event that is triggered before sorting happens, in that case I can build my own sorting mechanism. Or are there other nifty ways of sorting tables like these?

sorting qtablewidget pyqt6