Solved How to sort Qtreewidgetitems by icon
-
I want to sort qtreewidgetitems by icon.As an example, I have 3 different types of icons, red, green and blue.. I want red icons should be in top of the item hierarchy..then green and then blue.. but items having red icons should be ordered by text too..same condition for green and blue.. how can I implement such feature??
-
Derive from QTreeeWidgetItem and reimplement QTreeWidgetItem::operator<(const QTreeWidgetItem &other
-
@Christian-Ehrlicher An example would be better..
-
class MyTreeWidgetItem : public QTreeWidgetItem { public: using QTreeWidgetItem::QTreeWidgetItem; bool operator< (const QTreeWidgetItem *other) const { // return whatever you want your sorting is. }
-
update: oh, @Christian-Ehrlicher beat me to it :)
Hi
I can offer some code i used to sort items that had mixed names and numbers.
To sort on images, you could add extra meta info using setData + UserRole on the model to
add an integer saying red,green,blue so you have something to compare as the icons them self
will be hackish.
ps. just some test code so its not pretty.class QListWidgetItemSort : public QListWidgetItem { static QCollator sorter; public: static void setNumericMode() { sorter.setNumericMode(true); } virtual bool operator<(const QListWidgetItem& other) const override { bool ok1, ok2; int me = text().toInt(&ok1); int him = other.text().toInt(&ok2); // if one is a number if (ok1 || ok2 ) { // if only one of them is a number if (!(ok1 && ok2)) { if (ok1) { return false; } return true; } } // return me < him; return sorter.compare(text(), other.text()) < 0; } };
-
@mrjj thanks.. solved