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. QSortFilterProxyModel is sorting only on based on string.

QSortFilterProxyModel is sorting only on based on string.

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 784 Views
  • 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
    Ayush Gupta
    wrote on 24 Jul 2019, 05:35 last edited by
    #1

    I am have TreeView in my code. Basically I insert all the data as a string only in QTTreeView.
    But some string are also number. So I need to sort those columns which are having number based on number sorting not on String sorting.

    I have a limitataion in my code that I cannot use QVariant.

    Is there any way I can write custom lessthan of QSortFilterProxyModel with which I can indentify which column is string and which column is number.

    Is there any way I can tell the QTTreeView that this column contains number and this column contain string without using QVariant?

    S 1 Reply Last reply 24 Jul 2019, 06:14
    0
    • A Ayush Gupta
      24 Jul 2019, 05:35

      I am have TreeView in my code. Basically I insert all the data as a string only in QTTreeView.
      But some string are also number. So I need to sort those columns which are having number based on number sorting not on String sorting.

      I have a limitataion in my code that I cannot use QVariant.

      Is there any way I can write custom lessthan of QSortFilterProxyModel with which I can indentify which column is string and which column is number.

      Is there any way I can tell the QTTreeView that this column contains number and this column contain string without using QVariant?

      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 24 Jul 2019, 06:14 last edited by
      #2

      @Ayush-Gupta said in QSortFilterProxyModel is sorting only on based on string.:

      Is there any way I can write custom lessthan of QSortFilterProxyModel with which I can indentify which column is string and which column is number.

      Yes, check if QString converts to int without an error. If it does, you've got a number.

      Is there any way I can tell the QTTreeView that this column contains number and this column contain string without using QVariant?

      You can add a custom role to your model (you need to return it in data() method of your model) and check it in your filter proxy.

      (Z(:^

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Ayush Gupta
        wrote on 24 Jul 2019, 06:29 last edited by
        #3

        @sierdzio said in QSortFilterProxyModel is sorting only on based on string.:

        You can add a custom role to your model (you need to return it in data() method of your model) and check it in your filter proxy.

        Can you expalin this further. How I can achieve this. May be a peice of code explaining this?

        S 1 Reply Last reply 24 Jul 2019, 06:39
        0
        • A Ayush Gupta
          24 Jul 2019, 06:29

          @sierdzio said in QSortFilterProxyModel is sorting only on based on string.:

          You can add a custom role to your model (you need to return it in data() method of your model) and check it in your filter proxy.

          Can you expalin this further. How I can achieve this. May be a peice of code explaining this?

          S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 24 Jul 2019, 06:39 last edited by
          #4

          @Ayush-Gupta said in QSortFilterProxyModel is sorting only on based on string.:

          @sierdzio said in QSortFilterProxyModel is sorting only on based on string.:

          You can add a custom role to your model (you need to return it in data() method of your model) and check it in your filter proxy.

          Can you expalin this further. How I can achieve this. May be a peice of code explaining this?

          If you have a custom subclass for your model (do you?), you can do this:

          // In some header
          enum CustomRole {
            ColumnHasNumbers = Qt::UserRole + 1
          }
          
          // In model:
          QVariant YourModelClass::data(const QModelIndex &index, int role) const
          {
          switch (role) {
          case Qt::DisplayRole:
            // ...
          case Qt::EditRole:
            // ...
          case ColumnHasNumbers:
            // You need to detect which column has only numbers either here (inefficient) or somewhere else, for example during initialization of the model
            if (columnHasNumbers)
              return true;
            else
              return false;
          }
          

          (Z(:^

          1 Reply Last reply
          2
          • A Offline
            A Offline
            Ayush Gupta
            wrote on 24 Jul 2019, 07:14 last edited by
            #5

            how can we return true or false? Seems data should return actual data column contains

            J S 2 Replies Last reply 24 Jul 2019, 07:20
            0
            • A Ayush Gupta
              24 Jul 2019, 07:14

              how can we return true or false? Seems data should return actual data column contains

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 24 Jul 2019, 07:20 last edited by
              #6

              @Ayush-Gupta Boolean can be converted to QVariant: https://doc.qt.io/qt-5/qvariant.html#QVariant-8
              So, the code should just work.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • A Ayush Gupta
                24 Jul 2019, 07:14

                how can we return true or false? Seems data should return actual data column contains

                S Offline
                S Offline
                sierdzio
                Moderators
                wrote on 24 Jul 2019, 07:27 last edited by
                #7

                @Ayush-Gupta said in QSortFilterProxyModel is sorting only on based on string.:

                how can we return true or false? Seems data should return actual data column contains

                Look at other roles: https://doc.qt.io/qt-5/qt.html#ItemDataRole-enum there is far more information that can be (and is) returned than just column contents: you can specify the font, colours, size hints and custom roles.

                (Z(:^

                1 Reply Last reply
                0

                1/7

                24 Jul 2019, 05:35

                • Login

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