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 data in QTableWidget
Forum Update on Monday, May 27th 2025

Sorting data in QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 2.1k 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.
  • eefesafakE Offline
    eefesafakE Offline
    eefesafak
    wrote on last edited by
    #1

    Hello,
    I would like to ask you a question.
    My goal is to sorting files in ascending order. I have chosen "SortingEnabled" true from mainwindow.ui. But "Lines" column is not sorted by ascending order.
    My output is here:
    Screenshot from 2022-05-20 15-37-44.png
    Thanks in advance!

    JonBJ 1 Reply Last reply
    0
    • eefesafakE eefesafak

      Hello,
      I would like to ask you a question.
      My goal is to sorting files in ascending order. I have chosen "SortingEnabled" true from mainwindow.ui. But "Lines" column is not sorted by ascending order.
      My output is here:
      Screenshot from 2022-05-20 15-37-44.png
      Thanks in advance!

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

      @eefesafak said in Sorting data in QTableWidget:

      But "Lines" column is not sorted by ascending order.

      ?? What row is not sorted by the value in Lines column, ascending, that you show? And as it happens this is ascending by either number or string, so either way it's sorted ascending....

      eefesafakE 1 Reply Last reply
      1
      • JonBJ JonB

        @eefesafak said in Sorting data in QTableWidget:

        But "Lines" column is not sorted by ascending order.

        ?? What row is not sorted by the value in Lines column, ascending, that you show? And as it happens this is ascending by either number or string, so either way it's sorted ascending....

        eefesafakE Offline
        eefesafakE Offline
        eefesafak
        wrote on last edited by
        #3

        @JonB said in Sorting data in QTableWidget:

        What row is not sorted by the value in Lines column, ascending, that you show? And as it happens this is ascending by either number or string, so either way it's sorted ascending...

        For example, On line 798 and 799, 11 is not greater than 110.

        JonBJ 1 Reply Last reply
        0
        • eefesafakE eefesafak

          @JonB said in Sorting data in QTableWidget:

          What row is not sorted by the value in Lines column, ascending, that you show? And as it happens this is ascending by either number or string, so either way it's sorted ascending...

          For example, On line 798 and 799, 11 is not greater than 110.

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

          @eefesafak
          Exactly, and since you asked for ascending 11 comes before 110 which comes before 111....

          Do you understand that "ascending" means the lowest values at the start (lowest index, visual top of the list) and highest values at the end (highest index, visual bottom of the list, and continuing to get higher if you scroll down to reveal more rows)?

          Try clicking the Lines header a second time, see what that looks like? That should be descending....

          eefesafakE 1 Reply Last reply
          0
          • JonBJ JonB

            @eefesafak
            Exactly, and since you asked for ascending 11 comes before 110 which comes before 111....

            Do you understand that "ascending" means the lowest values at the start (lowest index, visual top of the list) and highest values at the end (highest index, visual bottom of the list, and continuing to get higher if you scroll down to reveal more rows)?

            Try clicking the Lines header a second time, see what that looks like? That should be descending....

            eefesafakE Offline
            eefesafakE Offline
            eefesafak
            wrote on last edited by
            #5

            @JonB I understand now. Thanks. Datas in "Lines" column are QString, not int.

            JonBJ M Christian EhrlicherC 3 Replies Last reply
            0
            • eefesafakE eefesafak

              @JonB I understand now. Thanks. Datas in "Lines" column are QString, not int.

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

              @eefesafak said in Sorting data in QTableWidget:

              Datas in "Lines" column are QString, not int.

              But you are missing/confusing the point. I wrote earlier:

              And as it happens this is ascending by either number or string, so either way it's sorted ascending....

              From what you show, it is sorted ascending in either/both of string or number. 110 > 11 holds whether you treat them as numbers or strings.

              Now, this will not always be the case for some numbers/strings. E.g. as number 20 > 3, but as a string 20 < 3. So it will depend which items in your list.

              QTableWidget will do the sort/comparison however you entered items into it. If you did so a strings it will sort by string, if your did so as numbers/ints it will sort by numeric value. You probably want the latter, so make sure you add the lines column values as numbers, not strings, into the QTableWidget....

              1 Reply Last reply
              3
              • eefesafakE eefesafak

                @JonB I understand now. Thanks. Datas in "Lines" column are QString, not int.

                M Offline
                M Offline
                mchinand
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • eefesafakE eefesafak

                  @JonB I understand now. Thanks. Datas in "Lines" column are QString, not int.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @eefesafak said in Sorting data in QTableWidget:

                  Datas in "Lines" column are QString, not int.

                  Because you added them as QString, not int.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  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