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 numbers for qstandarditemmodel

how to sort numbers for qstandarditemmodel

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 3.8k 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.
  • C Offline
    C Offline
    clementNg
    wrote on last edited by
    #1
    QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
      proxyModel->setSourceModel(this->customWidget->model());
      this->customWidget->ui->valueTableView()->setModel(proxyModel);
      this->customWidget->model()->sort(0);
    

    My data would be number and I want to sort them.
    e.g. 6, 22 , 2, 64
    result: 2, 22, 6, 64
    seems that the result is sorting string. Does anyone can give some examples for sorting number? Thank you.

    JonBJ 1 Reply Last reply
    0
    • C clementNg
      QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
        proxyModel->setSourceModel(this->customWidget->model());
        this->customWidget->ui->valueTableView()->setModel(proxyModel);
        this->customWidget->model()->sort(0);
      

      My data would be number and I want to sort them.
      e.g. 6, 22 , 2, 64
      result: 2, 22, 6, 64
      seems that the result is sorting string. Does anyone can give some examples for sorting number? Thank you.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @clementNg
      Are you sure you added the data items as numbers, not text, into the model? I can't remember, but it might be that they are treated as numbers if you do so, though possibly not...

      ...In which case, subclass QSortFilterProxyModel and override virtual https://doc.qt.io/qt-5/qsortfilterproxymodel.html#lessThan to convert arguments to numbers and do integer comparison.

      An alternative is to use https://doc.qt.io/qt-5/qsortfilterproxymodel.html#sortRole-prop to change the role used for sorting from DisplayRole to one for which you make your model's data(index, role) accessor return a number, if you prefer that approach.

      C 1 Reply Last reply
      0
      • JonBJ JonB

        @clementNg
        Are you sure you added the data items as numbers, not text, into the model? I can't remember, but it might be that they are treated as numbers if you do so, though possibly not...

        ...In which case, subclass QSortFilterProxyModel and override virtual https://doc.qt.io/qt-5/qsortfilterproxymodel.html#lessThan to convert arguments to numbers and do integer comparison.

        An alternative is to use https://doc.qt.io/qt-5/qsortfilterproxymodel.html#sortRole-prop to change the role used for sorting from DisplayRole to one for which you make your model's data(index, role) accessor return a number, if you prefer that approach.

        C Offline
        C Offline
        clementNg
        wrote on last edited by
        #3

        @JonB
        Thank you for your reply.
        How can i set item as number? It can only insert string for the QStandardItem.

        Would you mind to give some explanations or examples for the second approach? What data should I set to sortRole so that It can sort by number?

        JonBJ 1 Reply Last reply
        0
        • C clementNg

          @JonB
          Thank you for your reply.
          How can i set item as number? It can only insert string for the QStandardItem.

          Would you mind to give some explanations or examples for the second approach? What data should I set to sortRole so that It can sort by number?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @clementNg
          Yes, it looks like QStandardItem only has setText()/text() methods.

          So simplest is: subclass QSortFilterProxyModel, and use your class, so that we can override lessThan(), which is what is called by Qt to do sorting. Then that will look something like:

          virtual bool MySortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override
          {
              if (source_left.isValid() && source_right.isValid())
                  if (source_left.column() == 0)    // whatever column(s) you store numbers in
                      return source_left.data().toInt() < source_right.data().toInt();
              return QSortFilterProxyModel::lessThan(source_left, source_right);
          }
          

          I haven't put all the error checking in, but the important thing is see how we make numeric columns compare toInt() on their values, while other columns call the base class, which does string comparison. This is how we make the sort respect numbers as numbers.

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

            @JonB said in how to sort numbers for qstandarditemmodel:

            Yes, it looks like QStandardItem only has setText()/text() methods.

            https://doc.qt.io/qt-5/qstandarditem.html#setData

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

            JonBJ 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              @JonB said in how to sort numbers for qstandarditemmodel:

              Yes, it looks like QStandardItem only has setText()/text() methods.

              https://doc.qt.io/qt-5/qstandarditem.html#setData

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Christian-Ehrlicher
              I didn't spot QStandardItemModel::setData() either. I don't know where I saw setText() from.

              So: do you know whether the default QSortFilterProxyModel::lessThan() will (try to) compare as numbers instead of strings (if both sides are found to be numbers?), or what?

              @clementNg
              How (which call exactly) puts your "numbers" into your (source) model?

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

                When the QVariant is an integer / double then it will be treated as such: https://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qabstractitemmodel.cpp.html#_ZN25QAbstractItemModelPrivate17isVariantLessThanERK8QVariantS2_N2Qt15CaseSensitivityEb

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

                JonBJ C 2 Replies Last reply
                3
                • Christian EhrlicherC Christian Ehrlicher

                  When the QVariant is an integer / double then it will be treated as such: https://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qabstractitemmodel.cpp.html#_ZN25QAbstractItemModelPrivate17isVariantLessThanERK8QVariantS2_N2Qt15CaseSensitivityEb

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher
                  Yep! Looks like it's all based on the type of the left item :)

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    When the QVariant is an integer / double then it will be treated as such: https://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qabstractitemmodel.cpp.html#_ZN25QAbstractItemModelPrivate17isVariantLessThanERK8QVariantS2_N2Qt15CaseSensitivityEb

                    C Offline
                    C Offline
                    clementNg
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher
                    you notify me that I have triggered to edit the row so the data converted from number to QString . If I didn't do anything and sort the list, it can be sorted. The main problem is to ensure the data type is number but I dun know how to guarantee that.

                    JonBJ 1 Reply Last reply
                    0
                    • C clementNg

                      @Christian-Ehrlicher
                      you notify me that I have triggered to edit the row so the data converted from number to QString . If I didn't do anything and sort the list, it can be sorted. The main problem is to ensure the data type is number but I dun know how to guarantee that.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @clementNg said in how to sort numbers for qstandarditemmodel:

                      The main problem is to ensure the data type is number but I dun know how to guarantee that.

                      Depending on where you get your number from, if it's a string, convert it to a number before setData(), like we said.

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        clementNg
                        wrote on last edited by
                        #11

                        I have one more question. are there any differences between
                        setData(QVariant::fromValue(integer)) and
                        setData(QVariant(integer)) ?

                        JonBJ 1 Reply Last reply
                        0
                        • C clementNg

                          I have one more question. are there any differences between
                          setData(QVariant::fromValue(integer)) and
                          setData(QVariant(integer)) ?

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #12

                          @clementNg
                          They will both store an integer, they are the same. But you don't need to do that conversion anyway. Just do int intValue = 999; setData(intValue); and that will work, it (C++) will do a QVariant(intValue) behind the scenes. And QString::toInt() will convert a string to an integer.

                          If you have subclassed the model, or the model items, write yourself a bool setIntValue(int value) and a int intValue() appropriately, and use those to set/get all your integer values (instead of setData()/data()), then you will know what it is you are storing in the data for sure.

                          1 Reply Last reply
                          1

                          • Login

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