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. Sorting is wrong when using QSortFilterProxyModel on number strings and getting wrong column text
Forum Updated to NodeBB v4.3 + New Features

Sorting is wrong when using QSortFilterProxyModel on number strings and getting wrong column text

Scheduled Pinned Locked Moved General and Desktop
13 Posts 5 Posters 8.2k 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.
  • L Offline
    L Offline
    ludde
    wrote on last edited by
    #4

    I remember having the same problem. At the time, the only solution I could come up with that worked was to subclass QSortFilterProxyModel, and provide my own definition of when an item was "larger than" another item.

    But if you can make the sorting use numbers instead of strings, as Franzk suggests, that should work too. But then you probably have to store the numbers as numbers in the model, not as strings.

    1 Reply Last reply
    0
    • U Offline
      U Offline
      umen242
      wrote on last edited by
      #5

      i dont have problem to store it as number its even better , but how ?
      do you mean in
      @them_model->item(iSelectedRow,0)->setData(..value .., which role? )@
      if yes which role should i use ?

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kkrzewniak
        wrote on last edited by
        #6

        Have you considered writing your own custom Item model? Or at least a custom QStandardItem subclass.

        From the doc of QStandardItem :
        @Reimplement data() and setData() if you want to perform custom handling of data queries and/or control how an item's data is represented.@

        Me, Grimlock, not "nice dino". ME BASH BRAINS!

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dialingo
          wrote on last edited by
          #7

          There is a SortRole property in the latest snapshot:
          http://doc.qt.nokia.com/4.7-snapshot/qstandarditemmodel.html
          This feature makes implementing your requirement very easy.

          1 Reply Last reply
          0
          • U Offline
            U Offline
            umen242
            wrote on last edited by
            #8

            is there any tutorial ? how can i use it ?

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dialingo
              wrote on last edited by
              #9

              define a sort role:
              @const int sortRole = Qt::UserRole +1;@

              QStandardItemModel lets you save custom values with setData and role = sortRole. For every column you can decide whether you want to hold QString or int.

              Tell the sort method what role to use for sorting: @myStandardItemModel.setSortRole(sortRole);@

              Now sorting works based on the values you have saved in every cell with sortRole.

              1 Reply Last reply
              0
              • U Offline
                U Offline
                umen242
                wrote on last edited by
                #10

                Hi all
                well thanks for the answer . it working .
                for the secound question i found the solution and it looks like this :
                @QString groupID = index.model()->index(index.row(), 0, index.parent()).data(Qt::UserRole).toString();
                QString groupName = index.model()->index(index.row(), 0, index.parent()).data(Qt::DataRole).toString();@

                but there is one more problem is how do i setData to column in index ( for example 3 ) in the selected row
                this dosn't work :
                @index.model()->index(index.row(), 3, index.parent()).setData(.......)@

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dialingo
                  wrote on last edited by
                  #11

                  You get a QModelIndex for the current cell via this method
                  @QModelIndex QAbstractItemView::currentIndex () @
                  If you really want the selected cells and not the current cell you can query the selection Model, which is available via the view.
                  Once you have the QModelIndex of the target cell you can call the models setData() method with it.

                  1 Reply Last reply
                  0
                  • U Offline
                    U Offline
                    umen242
                    wrote on last edited by
                    #12

                    well this is the thing that i dont have the index of the cell i want to change only the row .

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      dialingo
                      wrote on last edited by
                      #13

                      The row is not enough, because you have a tree and the hierarchy is also needed.
                      You need to have a QModelIndex of a cell which is close to where you want to go.
                      If you have a QModelIndex of the parent item you can use
                      @QModelIndex::child ( int row, int column ) @
                      If you have a QModelIndex of a sibling, for example an item in the same row you would use
                      @QModelIndex::sibling ( int row, int column ) @

                      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