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 is not Properly.

QTableView Sorting is not Properly.

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 2.9k Views 2 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.
  • S Offline
    S Offline
    Sebastian
    wrote on last edited by
    #1

    Dear All,
    I have A QTableView with different columns and I have a QComboBox with items as QTableView column name.what i want like when we change the column name from comboBox then it should sort the items in Ascending Order.so for this the code is for sorting is

    m_sortModel = new QSortFilterProxyModel;
    m_tableView = new QTableView(this);
    m_sortModel->setSourceModel(new QAbstractTableModel);
    m_tableView->setModel(m_sortModel);
    m_tableView->setSortingEnabled(true);
    

    The above code is sorting the item but not the proper way what i want. Like if the item inside the column is 1,2,3,10,11,3 then it will sort like 1,10,11,2,3,3. so thats the problem.

    what i want it should sort like 1,2,3,3,10,11.

    Please if you have any idea or suggestion.
    TIA

    Venkatesh VV 1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Your model sorts by string instead of by value. Make sure you convert the values to int when sorting.

      (Z(:^

      1 Reply Last reply
      0
      • S Sebastian

        Dear All,
        I have A QTableView with different columns and I have a QComboBox with items as QTableView column name.what i want like when we change the column name from comboBox then it should sort the items in Ascending Order.so for this the code is for sorting is

        m_sortModel = new QSortFilterProxyModel;
        m_tableView = new QTableView(this);
        m_sortModel->setSourceModel(new QAbstractTableModel);
        m_tableView->setModel(m_sortModel);
        m_tableView->setSortingEnabled(true);
        

        The above code is sorting the item but not the proper way what i want. Like if the item inside the column is 1,2,3,10,11,3 then it will sort like 1,10,11,2,3,3. so thats the problem.

        what i want it should sort like 1,2,3,3,10,11.

        Please if you have any idea or suggestion.
        TIA

        Venkatesh VV Offline
        Venkatesh VV Offline
        Venkatesh V
        wrote on last edited by
        #3

        Hi @Sebastian,

        for ex:- string(1,2,3,10,11,3 )

        convert the string to list of integer
        QList<int> s = {1,2,3,10,11,13}
        Sort the list:- s = {1,2,3,3,10,11}
        Convert it to string again then set it to model.

        1 Reply Last reply
        1
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          new QAbstractTableModel this can't compile

          Make sure you convert the values to int when sorting.

          I disagree, you should store the values as numbers and convert them to string only if you absolutely, positively need them as string.

          I suspect you are having the same problem as this nice chap

          "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

          sierdzioS 1 Reply Last reply
          0
          • VRoninV VRonin

            new QAbstractTableModel this can't compile

            Make sure you convert the values to int when sorting.

            I disagree, you should store the values as numbers and convert them to string only if you absolutely, positively need them as string.

            I suspect you are having the same problem as this nice chap

            sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            @VRonin said in QTableView Sorting is not Properly.:

            I disagree, you should store the values as numbers and convert them to string only if you absolutely, positively need them as string.

            Yes, agreed.

            (Z(:^

            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