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. Sort QTreeView on multiple columns
Forum Updated to NodeBB v4.3 + New Features

Sort QTreeView on multiple columns

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 840 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
    AntriX
    wrote on last edited by
    #1

    Hi,

    I Have a tree view which contains data in table format like:
    Col1 Col2 Col3 Col4 ...
    H1
    B C D
    E F G
    H2
    H I J
    K L M

    I want to sort tree data based on multiple columns. Lets say on col1 and col2.
    I am using QSortFilterProxyModel to sort. I have implemented my own lessThan to sort as per my requirements.
    I have tried sorting like:
    m_TreeView->sortByColumn(1, Qt::AscendingOrder);
    m_TreeView->sortByColumn(0, Qt::AscendingOrder);
    so that col2 will be sorted followed by col1 but it didn't worked.
    Can Anyone please suggest right approach to implement this?

    A JonBJ 2 Replies Last reply
    0
    • A AntriX

      Hi,

      I Have a tree view which contains data in table format like:
      Col1 Col2 Col3 Col4 ...
      H1
      B C D
      E F G
      H2
      H I J
      K L M

      I want to sort tree data based on multiple columns. Lets say on col1 and col2.
      I am using QSortFilterProxyModel to sort. I have implemented my own lessThan to sort as per my requirements.
      I have tried sorting like:
      m_TreeView->sortByColumn(1, Qt::AscendingOrder);
      m_TreeView->sortByColumn(0, Qt::AscendingOrder);
      so that col2 will be sorted followed by col1 but it didn't worked.
      Can Anyone please suggest right approach to implement this?

      A Offline
      A Offline
      AntriX
      wrote on last edited by
      #2

      @AntriX said in Sort QTreeView on multiple columns:

      Col1 Col2 Col3 Col4 ...
      H1
      B C D
      E F G
      H2
      H I J
      K L M

      Col1       Col2     Col3     Col4 
       H1
                  B        C         D
                  E        F         G
       H2
                  H        I         J
                  K        L         M
      

      Tree structure looks like one shown above.

      1 Reply Last reply
      0
      • A AntriX

        Hi,

        I Have a tree view which contains data in table format like:
        Col1 Col2 Col3 Col4 ...
        H1
        B C D
        E F G
        H2
        H I J
        K L M

        I want to sort tree data based on multiple columns. Lets say on col1 and col2.
        I am using QSortFilterProxyModel to sort. I have implemented my own lessThan to sort as per my requirements.
        I have tried sorting like:
        m_TreeView->sortByColumn(1, Qt::AscendingOrder);
        m_TreeView->sortByColumn(0, Qt::AscendingOrder);
        so that col2 will be sorted followed by col1 but it didn't worked.
        Can Anyone please suggest right approach to implement this?

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @AntriX said in Sort QTreeView on multiple columns:

        but it didn't worked.

        What happened?

        sortByColumn() will only sort by one column. You cannot call it multiple times to produce sort by multiple columns. You have to implement that logic yourself (via your lessThan() taking into account secondary columns).

        A 1 Reply Last reply
        2
        • JonBJ JonB

          @AntriX said in Sort QTreeView on multiple columns:

          but it didn't worked.

          What happened?

          sortByColumn() will only sort by one column. You cannot call it multiple times to produce sort by multiple columns. You have to implement that logic yourself (via your lessThan() taking into account secondary columns).

          A Offline
          A Offline
          AntriX
          wrote on last edited by
          #4

          @JonB @JonB Thank you. Yes I was also thinking to implement it through lessThan function.
          I can sort columns by clicking on column header.
          Can you please guide how can I always sort column1 first and then sort the clicked column accordingly or vice-versa. Please share if you have any refrence code.

          JonBJ 1 Reply Last reply
          0
          • A AntriX

            @JonB @JonB Thank you. Yes I was also thinking to implement it through lessThan function.
            I can sort columns by clicking on column header.
            Can you please guide how can I always sort column1 first and then sort the clicked column accordingly or vice-versa. Please share if you have any refrence code.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @AntriX
            You need to design your own way for the user to tell you that they want such-and-such column as the secondary sort column. Then in your lessThan() if 2 values compare equal on the primary column look up the values in the secondary column and compare those to determine what it should return when the primary columns are equal.

            A 1 Reply Last reply
            0
            • JonBJ JonB

              @AntriX
              You need to design your own way for the user to tell you that they want such-and-such column as the secondary sort column. Then in your lessThan() if 2 values compare equal on the primary column look up the values in the secondary column and compare those to determine what it should return when the primary columns are equal.

              A Offline
              A Offline
              AntriX
              wrote on last edited by
              #6

              @JonB Thank you. I have done by sorting the data while filling the model which is meeting my requirement.

              1 Reply Last reply
              0
              • A AntriX has marked this topic as solved on

              • Login

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