Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to sort Qtreewidgetitems by icon
Forum Updated to NodeBB v4.3 + New Features

How to sort Qtreewidgetitems by icon

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 715 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abdullahzubair109
    wrote on last edited by
    #1

    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??

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Derive from QTreeeWidgetItem and reimplement QTreeWidgetItem::operator<(const QTreeWidgetItem &other

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      A 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        Derive from QTreeeWidgetItem and reimplement QTreeWidgetItem::operator<(const QTreeWidgetItem &other

        A Offline
        A Offline
        abdullahzubair109
        wrote on last edited by
        #3

        @Christian-Ehrlicher An example would be better..

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4
          class MyTreeWidgetItem : public QTreeWidgetItem
          {
           public:
            using QTreeWidgetItem::QTreeWidgetItem;
            bool operator< (const QTreeWidgetItem *other) const
            {
              // return whatever you want your sorting is.
            }
          

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            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;
              }
            };
            
            A 1 Reply Last reply
            1
            • mrjjM mrjj

              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;
                }
              };
              
              A Offline
              A Offline
              abdullahzubair109
              wrote on last edited by
              #6

              @mrjj thanks.. solved

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved