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. Query in one requirement in Sorting columns in QtableView
Qt 6.11 is out! See what's new in the release blog

Query in one requirement in Sorting columns in QtableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 3.2k Views 1 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    I am using a QTableView and have two columns in it

    1. Index - displays the row number
    2. other is name of object .

    For example
    Index Object
    0 /obj0
    1 /obj1
    2 /obj2
    3 /obj3

    I am using
    tView = new QTableView
    mymodel = new QAbstractTableModel
    proxyModel = new QSortFilterProxyModel(this);
    proxyModel->setSourceModel(mymodel);
    proxyModel->sort(0,Qt::AscendingOrder);
    tView->setModel(d_proxyModel);

    When we sort the second column , i,e Object name , the index column also changes. and the view is as in example
    3 /obj3
    2 /obj2
    1 /obj1
    0 /obj0

    The requirement is there any way the Index column remains same for example after sorting Object column , the view remains

    0 /obj3
    1 /obj2
    2 /obj1
    3 /obj0

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      Qt Enthusiast
      wrote on last edited by
      #2

      I am using a QTableView and have two columns in it

      Index - displays the row number
      other is name of object .
      For example
      Index Object
      0 /obj0
      1 /obj1
      2 /obj2
      3 /obj3

      I am using
      tView = new QTableView
      mymodel = new QAbstractTableModel
      proxyModel = new QSortFilterProxyModel(this);
      proxyModel->setSourceModel(mymodel);
      proxyModel->sort(0,Qt::AscendingOrder);
      tView->setModel(d_proxyModel);

      When we sort the second column , i,e Object name , the index column also changes. and the view is as in example
      3 /obj3
      2 /obj2
      1 /obj1
      0 /obj0

      The requirement is there any way the Index column remains same for example after sorting Object column , the view remains

      0 /obj3
      1 /obj2
      2 /obj1
      3 /obj0

      Please note I have gone through following example https://doc.qt.io/archives/4.6/itemviews-customsortfiltermodel.html but could solve my problem

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

        I you only want to display the row number you should use setHeaderData() on the model and use QHeaderView in the view

        "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
        0
        • Q Offline
          Q Offline
          Qt Enthusiast
          wrote on last edited by
          #4

          I used , but I am facin certain issues . Please see my requirement in details

          I have a application , in which I have a QWidget , The QWidget is divided into two parts using a QSplitter with vertical orientation.

          The two parts are

          1. QtreeWidget - onleft

          2. QtableView on right.

          The model used in QtableView is inherited from QAbstractTablemodel

          When we select a row in QtreeWidget , the QtableView on right should be updated. The QtableView should display row number in ascending order and one column should display the object names . To display the row numbers , I am using setHeaderData of QAbstractTablemodel for vertical headers I am facing some issues in using setHeaderData for vertical headers.

          Issues1
          I am also using QSortFilterProxyModel , to sort the object name column .The issue i am facing , when I sort the column with object names by clicking on horizontal header of the column and then If I select the row , the row number in the vertical header changes

          Is there any way by the , I can display the row numbers in the vertical headers that do not change (while sorting the object names columns) .

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            Qt Enthusiast
            wrote on last edited by
            #5

            @VRonin said:

            setHeaderData

            Could you proive some example of usage of setHeaderData for vertical headers

            1 Reply Last reply
            0
            • A Offline
              A Offline
              alex_malyu
              wrote on last edited by
              #6

              @Qt-Enthusiast said:

              QSortFilterProxyModel

              Subclass QSortFilterProxyModel override headerData method
              to return section number+1 for vertical orientation for display role

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                Qt Enthusiast
                wrote on last edited by
                #7

                My code is something like this

                class myModel:QAbstractTablemodel {
                }

                tableModel = new myModel(db);
                tableView = new QtableView;
                if (db->isSortingEnabled()) {
                proxyModel = new QSortFilterProxyModel(this);
                d_proxyModel->setSourceModel(myModel);
                proxyModel->sort(0,Qt::AscendingOrder);
                tableView->setModel(proxyModel);

                tableView->sortByColumn(0,Qt::AscendingOrder);
                if( tableView->verticalHeader()) {
                   tableView->verticalHeader()->setSortIndicator(0,Qt::AscendingOrder);
                }
                

                } else {
                tableView->setModel(myModel);
                }

                QVariant myModel::data(const QModelIndex& index, int role) const {

                }
                QVariant myModel::rowCount(const QModelIndex& index, int role) const {
                return rowMap.size();
                }

                QVariant myModel::columnCount(const QModelIndex& index, int role) const {
                return colMap.size();
                }

                /*to display headers in the tableview */
                QVariant myrModel::headerData(int section ,Qt::Orientation orientation, int role) const
                {
                if (role == Qt::DisplayRole && (orientation == Qt::Horizontal) && section < COL_COUNT ) {
                return QString((str_t)getColumnHeader(section));
                }

                if (role == Qt::DisplayRole && (orientation == Qt::Vertical)) {
                return QVariant(section);
                }
                return QVariant();
                }

                I am getting two problems in it . One is vertical headers are displayed in descensding order and other is that when I select the row after sorting the column ,the vertcial header text changes

                Please provide help to solve the problem

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  Qt Enthusiast
                  wrote on last edited by
                  #8

                  I am not able to solve this problem . Basically I want my row numbers to always in sequential order , even if user sorts a column by clicking on horizontal header.The vertical header should be static and it should display the row numbers in sequential orders . I tried the suggestions but they are not working . If some one can guide me with a example , it will be helpful

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

                    It is already built in if you use a table view.
                    check Table Model Example, just add view1->setSortingEnabled(true); to enable the row sorting

                    "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
                    0

                    • Login

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