2

I am trying to sort values numerically instead of alphabetically in my QTreeWidget. Right now It's comparing as two strings, what I need is for it to compare as two integers.

What I have so far:

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QSql>
//more include...
#include <QTreeWidget>

extern QSqlDatabase db;
//more extern...

namespace Ui {
class mainwindow;
class TreeWidgetItem;
}

class TreeWidgetItem : public QTreeWidgetItem
{
    Q_OBJECT

public:
    TreeWidgetItem(QTreeWidget *tree) : QTreeWidgetItem(tree)  {}
    TreeWidgetItem(QTreeWidget * parent, const QStringList & strings)
                   : QTreeWidgetItem (parent,strings)  {}
    bool operator< (const QTreeWidgetItem &other) const
    {
        int column = treeWidget()->sortColumn();
        return text(column).toInt() < other.text(column).toInt();
    }
};

class mainwindow : public QMainWindow
{
    Q_OBJECT

public:
    //...

but it doesn't work, any idea what else i need to do? Or am I completely off?

throwaway2013
  • 440
  • 8
  • 19
  • 1
    [same question here](http://stackoverflow.com/questions/363200/is-it-possible-to-sort-numbers-in-a-qtreewidget-column?rq=1) The only difference is that `bool operator< (const ...` is private in that accepted answer... –  Jul 03 '13 at 02:00
  • As long as it's being accessed in the same class it being private or public shouldn't make a difference in this context afaik. – throwaway2013 Jul 12 '13 at 12:59
  • 2
    May be you should take a look at QSortFilterProxyModel class http://qt-project.org/doc/qt-5.0/qtcore/qsortfilterproxymodel.html. There is an example how to use this for a TreeView widget. Basicly you have to derive from that class and reimplement some methods. – bkausbk Jul 16 '13 at 06:23

1 Answers1

0

Have you checked what the function is returning (ie. look at the values in a debugger)? I'm willing to bet the answer will be obvious when viewed through a different lens.

jhammond
  • 1,916
  • 2
  • 15
  • 29