Actually, both QTableWidget and QTreeWidget use special sort operators provided by QTableWidgetItem and QTreeWidgetItem, respectively. Unfortunately these sort operators are kind of outward:
{
const QVariant v1
= data
(Qt
::DisplayRole), v2
= other.
data(Qt
::DisplayRole);
if (QTableModel::canConvertToDouble(v1) && QTableModel::canConvertToDouble(v2))
return v1.toDouble() < v2.toDouble();
return v1.toString() < v2.toString();
}
bool QTableWidgetItem::operator<(const QTableWidgetItem &other) const
{
const QVariant v1 = data(Qt::DisplayRole), v2 = other.data(Qt::DisplayRole);
if (QTableModel::canConvertToDouble(v1) && QTableModel::canConvertToDouble(v2))
return v1.toDouble() < v2.toDouble();
return v1.toString() < v2.toString();
}
To copy to clipboard, switch view to plain text mode
{
int column = view ? view->sortColumn() : 0;
return text(column) < other.text(column);
}
bool QTreeWidgetItem::operator<(const QTreeWidgetItem &other) const
{
int column = view ? view->sortColumn() : 0;
return text(column) < other.text(column);
}
To copy to clipboard, switch view to plain text mode
The above code is from Qt 4.3.2. As you can see, QTreeWidgetItem always sorts based on text. QTableWidgetItem is slightly better with its numerical check. However, since they are virtual, you can always subclass and reimplement them. I just wonder why didn't Trolls make them more intelligent, for example something like this (a simplified version of what QSortFilterProxyModel does):
{
int column = treeWidget() ? treeWidget()->sortColumn() : 0;
QVariant l
= data
(column, Qt
::DisplayRole);
QVariant r
= data
(column, Qt
::DisplayRole);
switch (l.type())
{
return l.toInt() < r.toInt();
return l.toUInt() < r.toUInt();
return l.toLongLong() < r.toLongLong();
return l.toULongLong() < r.toULongLong();
return l.toDouble() < r.toDouble();
return l.toChar() < r.toChar();
return l.toDate() < r.toDate();
return l.toTime() < r.toTime();
return l.toDateTime() < r.toDateTime();
default:
return l.toString().compare(r.toString(), Qt::CaseSensitive) < 0;
}
return false;
}
bool MyTreeWidgetItem::operator<(const QTreeWidgetItem &other) const
{
int column = treeWidget() ? treeWidget()->sortColumn() : 0;
QVariant l = data(column, Qt::DisplayRole);
QVariant r = data(column, Qt::DisplayRole);
switch (l.type())
{
case QVariant::Invalid:
return (r.type() == QVariant::Invalid);
case QVariant::Int:
return l.toInt() < r.toInt();
case QVariant::UInt:
return l.toUInt() < r.toUInt();
case QVariant::LongLong:
return l.toLongLong() < r.toLongLong();
case QVariant::ULongLong:
return l.toULongLong() < r.toULongLong();
case QVariant::Double:
return l.toDouble() < r.toDouble();
case QVariant::Char:
return l.toChar() < r.toChar();
case QVariant::Date:
return l.toDate() < r.toDate();
case QVariant::Time:
return l.toTime() < r.toTime();
case QVariant::DateTime:
return l.toDateTime() < r.toDateTime();
case QVariant::String:
default:
return l.toString().compare(r.toString(), Qt::CaseSensitive) < 0;
}
return false;
}
To copy to clipboard, switch view to plain text mode
PS. http://trolltech.com/developer/task-...ntry&id=166873
Bookmarks