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

    sierdzioS 1 Reply Last reply
    0
    • A Ayush Gupta

      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?

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on 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 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?

        sierdzioS 1 Reply Last reply
        0
        • A Ayush Gupta

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

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on 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 last edited by
            #5

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

            jsulmJ sierdzioS 2 Replies Last reply
            0
            • A Ayush Gupta

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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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

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

                sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on 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

                • Login

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