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. Qtableview sorting issue

Qtableview sorting issue

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 858 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.
  • E eswaramrth03

    I am using qtableview to dispaly the data in my application . The data are stored in the QStandardItemModel . For sorting my data, i am using the following function

    QStandardItemModel *grpModel;
    grpModel->sort(1,Qt::AscendingOrder);
    

    Qtableview also have editing option , While editing the sorting will not work well, Please check the below image
    2.png

    sometimes i am having following error and application crashed
    1.png

    Please give the solution for this.

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @eswaramrth03 said in Qtableview sorting issue:

    QStandardItemModel *grpModel;
    grpModel->sort(1,Qt::AscendingOrder);

    You know that you have to assign a valid pointer to grpModel? C++ basics.

    QStandardItemModel *grpModel = new QStandardItemModel();
    grpModel->sort(1,Qt::AscendingOrder);
    

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

    E 1 Reply Last reply
    1
    • jsulmJ jsulm

      @eswaramrth03 said in Qtableview sorting issue:

      QStandardItemModel *grpModel;
      grpModel->sort(1,Qt::AscendingOrder);

      You know that you have to assign a valid pointer to grpModel? C++ basics.

      QStandardItemModel *grpModel = new QStandardItemModel();
      grpModel->sort(1,Qt::AscendingOrder);
      
      E Offline
      E Offline
      eswaramrth03
      wrote on last edited by
      #3

      @jsulm
      i am done on the same way only.

      jsulmJ 1 Reply Last reply
      0
      • E eswaramrth03

        @jsulm
        i am done on the same way only.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @eswaramrth03 said in Qtableview sorting issue:

        i am done on the same way only

        What do you mean? Do you mean you do it like I did and still have the crash?
        Did you use debugger to get more information about the crash?
        Do you have a stack trace after crash?

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

        E 1 Reply Last reply
        1
        • jsulmJ jsulm

          @eswaramrth03 said in Qtableview sorting issue:

          i am done on the same way only

          What do you mean? Do you mean you do it like I did and still have the crash?
          Did you use debugger to get more information about the crash?
          Do you have a stack trace after crash?

          E Offline
          E Offline
          eswaramrth03
          wrote on last edited by eswaramrth03
          #5

          @jsulm i already assign the valid pointer as mention like you. If i debug my application , sometimes it will show the below message in the sort function line. If you press abort or retry it will lead to crash the application
          420dc58b-71b0-4c8f-8c0f-17dd2fa85b63.png. if it will not show the message means the wrong order will be displayed in the view as below.
          bf7bfe67-1410-46e5-b4bd-06328d0ece06.png

          i am converting Qstring to double for my application and store it in to list for my application calculation on backend.

          jsulmJ 1 Reply Last reply
          0
          • E eswaramrth03

            @jsulm i already assign the valid pointer as mention like you. If i debug my application , sometimes it will show the below message in the sort function line. If you press abort or retry it will lead to crash the application
            420dc58b-71b0-4c8f-8c0f-17dd2fa85b63.png. if it will not show the message means the wrong order will be displayed in the view as below.
            bf7bfe67-1410-46e5-b4bd-06328d0ece06.png

            i am converting Qstring to double for my application and store it in to list for my application calculation on backend.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @eswaramrth03 said in Qtableview sorting issue:

            If i debug the my application , it will show the below message

            Please post the stack trace. How else should anybody know why it crashes?

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

            1 Reply Last reply
            1
            • nageshN Offline
              nageshN Offline
              nagesh
              wrote on last edited by
              #7

              @eswaramrth03 first column is sorted in "string order", as data in the first column is QString.
              How did you populate model data?
              check for the answer in the following thread for the sorting.
              https://forum.qt.io/topic/115420/sorting-numerical-values-in-qtableview-with-widgets

              VRoninV 1 Reply Last reply
              2
              • nageshN nagesh

                @eswaramrth03 first column is sorted in "string order", as data in the first column is QString.
                How did you populate model data?
                check for the answer in the following thread for the sorting.
                https://forum.qt.io/topic/115420/sorting-numerical-values-in-qtableview-with-widgets

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #8

                @nagesh Agreed!

                Basically always avoid using the constructors QStandardItem(const QIcon &icon, const QString &text) and QStandardItem(const QString &text) as most of the times they don't do what you want them to do

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                E 1 Reply Last reply
                0
                • VRoninV VRonin

                  @nagesh Agreed!

                  Basically always avoid using the constructors QStandardItem(const QIcon &icon, const QString &text) and QStandardItem(const QString &text) as most of the times they don't do what you want them to do

                  E Offline
                  E Offline
                  eswaramrth03
                  wrote on last edited by
                  #9

                  @VRonin i didn't use constructor as mention like you. In one case , I didn't get error as well as sort issue

                  If you give two digits instead of one digits , it will give proper result. Sample data

                  00
                  05
                  09
                  11
                  20
                  instead of
                  0
                  5
                  9
                  11
                  20

                  But in my application the user input should be either one digit or two digit.

                  JonBJ 1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #10

                    This is because you are storing numbers as stings so they get sorted in alphabetical order. Do not convert numbers to strings

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    3
                    • E eswaramrth03

                      @VRonin i didn't use constructor as mention like you. In one case , I didn't get error as well as sort issue

                      If you give two digits instead of one digits , it will give proper result. Sample data

                      00
                      05
                      09
                      11
                      20
                      instead of
                      0
                      5
                      9
                      11
                      20

                      But in my application the user input should be either one digit or two digit.

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

                      @eswaramrth03
                      As @VRonin has said. Please read the link @nagesh gave you and follow that QStandardItem::setData() pattern to correctly store numbers.

                      1 Reply Last reply
                      3

                      • Login

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