Qt-interest Archive, February 2007
Re: Sort numerically QTableWidget
Message 1 in thread
> My application required sorting of all rows, function "sortByColumn( )"
> sort all row alphabetically by comparing
> string. I want to sort it numerically. Please suggest some way.
Hi,
The easiest and proper solution is to set the data as integers, not as text:
QTableWidgetItem* item = ...
item->setText("2"); // not like this
item->setData(Qt::DisplayRole, 2); // but like this
The sorting will work correctly out of the box. Also, editable items
have spin boxes instead of line edits as editors.
Another solution (or more like a workaround) is to inherit
QTableWidgetItem and reimplement QTableWidgetItem::operator<().
--
[ signature omitted ]