Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Qtableview sorting issue

    General and Desktop
    5
    11
    387
    Loading More Posts
    • 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 last edited by

      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.

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @eswaramrth03 last edited by

        @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 Reply Quote 1
        • E
          eswaramrth03 @jsulm last edited by

          @jsulm
          i am done on the same way only.

          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @eswaramrth03 last edited by

            @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 Reply Quote 1
            • E
              eswaramrth03 @jsulm last edited by 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.

              jsulm 1 Reply Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion @eswaramrth03 last edited by

                @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 Reply Quote 1
                • nagesh
                  nagesh last edited by

                  @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

                  VRonin 1 Reply Last reply Reply Quote 2
                  • VRonin
                    VRonin @nagesh last edited by

                    @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 Reply Quote 0
                    • E
                      eswaramrth03 @VRonin last edited by

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

                      JonB 1 Reply Last reply Reply Quote 0
                      • VRonin
                        VRonin last edited by

                        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 Reply Quote 3
                        • JonB
                          JonB @eswaramrth03 last edited by JonB

                          @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 Reply Quote 3
                          • First post
                            Last post