You can do it fairly easily with the convenience view as well:
{
Q_OBJECT
public:
{
// disable built-in sorting
setSortingEnabled(false);
// use our own sorting method instead
header()->setSortIndicatorShown(true);
header()->setClickable(true);
connect(header(), SIGNAL(sectionClicked(int)), this, SLOT(customSortByColumn(int)));
customSortByColumn(header()->sortIndicatorSection());
}
public slots:
void customSortByColumn(int column)
{
// here you can get the order
Qt::SortOrder order = header()->sortIndicatorOrder();
// and sort the items
sortItems(column, order);
// to get more control over actual sorting of items,
// reimplement QTreeWidgetItem::operator<()
}
};
class MyTreeWidget : public QTreeWidget
{
Q_OBJECT
public:
MyTreeWidget(QWidget* parent = 0)
: QTreeWidget(parent)
{
// disable built-in sorting
setSortingEnabled(false);
// use our own sorting method instead
header()->setSortIndicatorShown(true);
header()->setClickable(true);
connect(header(), SIGNAL(sectionClicked(int)), this, SLOT(customSortByColumn(int)));
customSortByColumn(header()->sortIndicatorSection());
}
public slots:
void customSortByColumn(int column)
{
// here you can get the order
Qt::SortOrder order = header()->sortIndicatorOrder();
// and sort the items
sortItems(column, order);
// to get more control over actual sorting of items,
// reimplement QTreeWidgetItem::operator<()
}
};
To copy to clipboard, switch view to plain text mode
Bookmarks