PDA

View Full Version : How to sort the treeWidget item according to size and date



santosh.kumar
22nd October 2007, 12:18
hi

I m using Qt4.2 on MAc 10.4
I have one application that contains one treeWidget. treeWidget contains three column.
first column contains filename, second column contains size of file, third column contains
creation date of file..

TreeWidget has already a function to sort the item...

treeWidget->sortItems(column, Qt::AscendingOrder);

But it will sort on the basis of Name order from ("AAAA" to "ZZZZ");

But i want to sort the second column according to size of file...so how i will sort the items according to size or date...

Can i do this using making subclass and making override functions sortItems and write
our own function to compare two item to each other...

But i m not clear....

if anybody know this ...help please

DeepDiver
22nd October 2007, 12:59
I usually work with QTreeView and QAbstractItemModel.

Based on this knowledge and the fact QTreeWidget is based on the Mode/View concept (http://doc.trolltech.com/4.3/model-view-programming.html) - try this:
Instead of setText use setData to pass the data to the widget.
Because setData uses QVariant, you can pass the size as long and the file date as QDateTime.
QTreeWidget will sort on the long/QDateTime and not on the string representation.

Good luck,

Tom

jpn
23rd October 2007, 10:02
Actually, both QTableWidget and QTreeWidget use special sort operators provided by QTableWidgetItem and QTreeWidgetItem, respectively. Unfortunately these sort operators are kind of outward:


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();
}



bool QTreeWidgetItem::operator<(const QTreeWidgetItem &other) const
{
int column = view ? view->sortColumn() : 0;
return text(column) < other.text(column);
}

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):


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;
}


PS. http://trolltech.com/developer/task-tracker/index_html?method=entry&id=166873

DeepDiver
23rd October 2007, 11:32
Hi jpn,

I just had a look at the code - this is very interesting how trolls have implemented sorting in QTreeWidget. Somehow a lazy impl ......

In stead of sub classing QTreeWidgetItem I would go for the model view concept. Using QTreeView and others gives you much more functionality - like sorting on the right data types.

Just my two cent ......

Tom