I was facing a similar problem, just a bit more complex than this one.
In a QTableWidget, in some columns I had simple numeric values and in others I wanted to show them with a " %" appended.
e.g. "65.32" -----> "65.32 %"
But when sorting I wanted the cells to be sorted numerically.
Thus I subclassed the QTableWidgetItem and then implemented the "<" operator to sort numbers and not strings.
{
if(text().contains("%"))
{
str1.chop(2);
str2.chop(2);
return str1.toDouble() < str2.toDouble();
}
else
{
return text().toDouble() < other.text().toDouble();
}
}
class MyTableWidgetItem : public QTableWidgetItem
{
if(text().contains("%"))
{
QString str1 = text();
str1.chop(2);
QString str2 = other.text();
str2.chop(2);
return str1.toDouble() < str2.toDouble();
}
else
{
return text().toDouble() < other.text().toDouble();
}
}
To copy to clipboard, switch view to plain text mode
Then while populating the table I passed instances of my custom items instead of the generic ones.
I learnt the basics for this technique from here.
Bookmarks