Results 1 to 4 of 4

Thread: How to sort the treeWidget item according to size and date

  1. #1
    Join Date
    May 2007
    Posts
    110
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Default How to sort the treeWidget item according to size and date

    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

  2. #2
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to sort the treeWidget item according to size and date

    I usually work with QTreeView and QAbstractItemModel.

    Based on this knowledge and the fact QTreeWidget is based on the Mode/View concept - 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/QDateTimeand not on the string representation.

    Good luck,

    Tom

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to sort the treeWidget item according to size and date

    Actually, both QTableWidget and QTreeWidget use special sort operators provided by QTableWidgetItem and QTreeWidgetItem, respectively. Unfortunately these sort operators are kind of outward:
    Qt Code:
    1. bool QTableWidgetItem::operator<(const QTableWidgetItem &other) const
    2. {
    3. const QVariant v1 = data(Qt::DisplayRole), v2 = other.data(Qt::DisplayRole);
    4. if (QTableModel::canConvertToDouble(v1) && QTableModel::canConvertToDouble(v2))
    5. return v1.toDouble() < v2.toDouble();
    6. return v1.toString() < v2.toString();
    7. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. bool QTreeWidgetItem::operator<(const QTreeWidgetItem &other) const
    2. {
    3. int column = view ? view->sortColumn() : 0;
    4. return text(column) < other.text(column);
    5. }
    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):
    Qt Code:
    1. bool MyTreeWidgetItem::operator<(const QTreeWidgetItem &other) const
    2. {
    3. int column = treeWidget() ? treeWidget()->sortColumn() : 0;
    4. QVariant l = data(column, Qt::DisplayRole);
    5. QVariant r = data(column, Qt::DisplayRole);
    6. switch (l.type())
    7. {
    8. case QVariant::Invalid:
    9. return (r.type() == QVariant::Invalid);
    10. case QVariant::Int:
    11. return l.toInt() < r.toInt();
    12. case QVariant::UInt:
    13. return l.toUInt() < r.toUInt();
    14. case QVariant::LongLong:
    15. return l.toLongLong() < r.toLongLong();
    16. case QVariant::ULongLong:
    17. return l.toULongLong() < r.toULongLong();
    18. case QVariant::Double:
    19. return l.toDouble() < r.toDouble();
    20. case QVariant::Char:
    21. return l.toChar() < r.toChar();
    22. case QVariant::Date:
    23. return l.toDate() < r.toDate();
    24. case QVariant::Time:
    25. return l.toTime() < r.toTime();
    26. case QVariant::DateTime:
    27. return l.toDateTime() < r.toDateTime();
    28. case QVariant::String:
    29. default:
    30. return l.toString().compare(r.toString(), Qt::CaseSensitive) < 0;
    31. }
    32. return false;
    33. }
    To copy to clipboard, switch view to plain text mode 

    PS. http://trolltech.com/developer/task-...ntry&id=166873
    J-P Nurmi

  4. The following user says thank you to jpn for this useful post:

    supergillis (3rd August 2008)

  5. #4
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to sort the treeWidget item according to size and date

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.