Important: Please read the Qt Code of Conduct -

Sort columns of QTreeWidget

  • Hello,

    I would like give the user the possibility to sort columns in a QTreeWidget.

    What I learned so far:

    • adding items with "myItem->setText( ... )" will sort the column in alphabetical order
    • adding items with "myItem->setData(0, Qt::DisplayRole, QVariant(.. double or QDate ..))" will sort the column according the data (numerial or by date)

    ... but ... (and now comes my problem)
    the data added using "setData( .. )" is represented in an internal way, e.g. double with a dot as decimal delimiter and ugly looking dates.

    I would like to display the data in a localized way (double with a comma, dates in format "dd.MM.yyyy") but still having the possibility to sort them correctly (not alphabetically).

    Is there any solution how to achieve this?

    Thanks a lot for your help !

  • hm i can think of two possibilities for your problem. But I havent tested any of them.

    First one:

    Implement your own QTreewidgetitem subclass, and implement @virtual bool operator< ( const QTreeWidgetItem & other ) const@ there.

    Second one:

    Make own Model and View. then use a QSortFilterProxyModel to sort the view

  • good!

  • Thanks, for that good idea.

    Within QT3 I also implemented a subclass of QListViewItem and implemented "virtual int compare( ... )" but I didn't find this within QT4.

    Your first solution is very close to the one I used to have, so I've tried this and it works pretty well !

    Thanks a lot for your help !

Log in to reply