I have a QTableWidget
, some columns are filled with text, some with numbers, these columns are fine to sort. But I also have a column with custom widgets, how should I enable sorting for these?
My first thought was of course to overload the '<'
method on the QTableWidgetItem
, but there are no QTableWidgetItem
. So what would be the best way to solve this?
QTableWidget
is not ideal for this case, consider usingQTableView
. Anyway, I will show you how to sort aQProgressBar
widget in aQTableWidget
.As you already said, you can overload the
<() operator
for this case. Let's say you have aQProgressBar
in column number 4. You have to overload the<()
operator.You have to subclass
QProgressBar
andQTableWidgetItem
.And then you can insert your QProgressBar like this in cell number 4.
Insert
QProgressBar
like thisLet's say you want to have simple text at cell 1 use
QTableWidgetItem
.If you want to sort numbers as well, for example in cell 0 use CustomTableWidgetItem, since we have implemented an if-statement as you can see above for sorting numbers and progressbar.
You can use this approach with other widgets as well, just subclass the correct widget, but in general it's better to use QTableView (MVC approach).
Here is a screenshot with
QTableWidget
andQProgressBar
sorting.