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 setSortingEnabled  forcing sortByColumn()

QtableView setSortingEnabled  forcing sortByColumn()

Scheduled Pinned Locked Moved Solved General and Desktop
qt5.9qtableviewqtableview c++sorting
23 Posts 5 Posters 29.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.
  • J JonB
    9 Nov 2017, 10:30

    @m.sue
    But the problem is, I think, detecting when/where it's called. Mostly we do, of course, want to do sorts from the model to the database; the problem is that we need to call QTableView.setSortingEnabled() and QTableView.sortByColumn(), but we don't want them both to do a database call, at least something like not one after the other.

    OK, let me ask you (I am a Qt beginner), please: how can I robustly determine if QSqlQueryModel.sort() is being called from/as a result of QTableView.setSortingEnabled()? That might give me a start on a solution.

    M Offline
    M Offline
    m.sue
    wrote on 9 Nov 2017, 10:42 last edited by
    #13

    Hi @JNBarchan

    I would set a flag in a model (derived class instance) before I call setSortingEnabled(true). The flag will make the sort function do nothing. After the call setSortingEnabled(true) I would set the flag back. So the next call of the sort function makes it work by default.

    -Michael.

    J 1 Reply Last reply 9 Nov 2017, 11:26
    1
    • M m.sue
      9 Nov 2017, 08:37

      Hi @mapuna

      Hmm, tableView->setSortingEnabled(true); sorts immediately. So, isn't it just enabled by pressing the appropiate column header by the user? I would think sorting is always possible but not done, by default. When the user presses a column header then sorting happens and the (sortingEnabled property is set to true). But no need to set it on your own.

      -Michael.

      M Offline
      M Offline
      mapuna
      wrote on 9 Nov 2017, 10:48 last edited by
      #14

      @m.sue While workig with the QtableView I found out that , if I am not explicitly setting the tableView->setSortingEnabled(true); during model/view initializations then just clicking on the column header do not sorts the table.

      Its only when in init part I explicitly set sortingenabled flag then only the clicking on columns sorts the data.I am using Qt5.9, can you tell me what wrong can I be doing.

      J 1 Reply Last reply 9 Nov 2017, 11:11
      0
      • M mapuna
        9 Nov 2017, 10:48

        @m.sue While workig with the QtableView I found out that , if I am not explicitly setting the tableView->setSortingEnabled(true); during model/view initializations then just clicking on the column header do not sorts the table.

        Its only when in init part I explicitly set sortingenabled flag then only the clicking on columns sorts the data.I am using Qt5.9, can you tell me what wrong can I be doing.

        J Offline
        J Offline
        JonB
        wrote on 9 Nov 2017, 11:11 last edited by JonB 11 Sept 2017, 11:11
        #15

        @mapuna
        I don't see you are doing anything "wrong". The whole point of the discussion above is that you do need to call tableView->setSortingEnabled(true) to enable column header sorting, and then as you have reported that will itself cause a sort to be executed at that point, just exactly as you said.

        Could you also kindly tell me, at least, what type of model you are using for the view --- is it a database (e.g. QSqlQuery), or is it your own in-memory data? Thank you.

        M 1 Reply Last reply 10 Nov 2017, 07:22
        0
        • M m.sue
          9 Nov 2017, 10:42

          Hi @JNBarchan

          I would set a flag in a model (derived class instance) before I call setSortingEnabled(true). The flag will make the sort function do nothing. After the call setSortingEnabled(true) I would set the flag back. So the next call of the sort function makes it work by default.

          -Michael.

          J Offline
          J Offline
          JonB
          wrote on 9 Nov 2017, 11:26 last edited by JonB 11 Sept 2017, 11:28
          #16

          @m.sue said in QtableView setSortingEnabled forcing sortByColumn():

          Hi @JNBarchan

          I would set a flag in a model (derived class instance) before I call setSortingEnabled(true). The flag will make the sort function do nothing. After the call setSortingEnabled(true) I would set the flag back. So the next call of the sort function makes it work by default.

          -Michael.

          Let me see if I understand what you are suggesting, with the flag on the model. Here is some pseudo-C++/C# :) :

          DerivedSqlQueryModel() : QSqlQueryModel()
          {
              bool doSort = true;
          
              override void sort(...)
              {
                  if (doSort)
                      base.sort(...);
              }
          }
          
          DerivedTableView(model) : QTableView(model)
          {
              override void setSortingEnabled(bool enabled)
              {
                  model.doSort = false;
                  base.setSortingEnabled(enabled);
                  model.doSort = true;
              }
          }
          

          Right? Thank you.

          1 Reply Last reply
          1
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 9 Nov 2017, 22:34 last edited by
            #17

            Hi,

            Maybe QSortFilterProxyModel might be of interest ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • J JonB
              9 Nov 2017, 11:11

              @mapuna
              I don't see you are doing anything "wrong". The whole point of the discussion above is that you do need to call tableView->setSortingEnabled(true) to enable column header sorting, and then as you have reported that will itself cause a sort to be executed at that point, just exactly as you said.

              Could you also kindly tell me, at least, what type of model you are using for the view --- is it a database (e.g. QSqlQuery), or is it your own in-memory data? Thank you.

              M Offline
              M Offline
              mapuna
              wrote on 10 Nov 2017, 07:22 last edited by
              #18

              @JNBarchan May be I am not clear about my problem, below is the summary for my issue:

              • I want to enable sorting for QtableView so I call tableView->setSortingEnabled(true) in init
              • But beacuse of above my View/Table gets sorted by first column (calling tableView->setSortingEnabled(true) makes immediate
                call to sortbycolumn)
              • I do not want the sorting to be done by default , I want the tableView->setSortingEnabled(true) to just set the flag and not call the sortByColumn so when I view the table data is not sorted by any column instead the user can then choose which column to use for sorting
              • Is it possible that I do not need to tableView->setSortingEnabled(true) do in init just to enable the sorting in table/view ?Withouth setting this flag will clicking on header of column be able to sort the tableview?
              • I use Qt5.9 with Class myModel : public QAbstractTableModel
              J 1 Reply Last reply 10 Nov 2017, 08:19
              0
              • M mapuna
                10 Nov 2017, 07:22

                @JNBarchan May be I am not clear about my problem, below is the summary for my issue:

                • I want to enable sorting for QtableView so I call tableView->setSortingEnabled(true) in init
                • But beacuse of above my View/Table gets sorted by first column (calling tableView->setSortingEnabled(true) makes immediate
                  call to sortbycolumn)
                • I do not want the sorting to be done by default , I want the tableView->setSortingEnabled(true) to just set the flag and not call the sortByColumn so when I view the table data is not sorted by any column instead the user can then choose which column to use for sorting
                • Is it possible that I do not need to tableView->setSortingEnabled(true) do in init just to enable the sorting in table/view ?Withouth setting this flag will clicking on header of column be able to sort the tableview?
                • I use Qt5.9 with Class myModel : public QAbstractTableModel
                J Offline
                J Offline
                JonB
                wrote on 10 Nov 2017, 08:19 last edited by JonB 11 Oct 2017, 08:21
                #19

                @mapuna
                On the contrary, I am saying: I do understand your issue, and yes it is a problem, for me too! I will try to be brief here, as we have already covered the detail if you read through above responses.

                It boils down to:

                • Yes, you do need to call tableView->setSortingEnabled(true) (somewhere) to enable sorting on the table.
                • No, it is not possible for tableView->setSortingEnabled(true) to just "set a flag" and not call sortByColumn(), it always calls that, unfortunately.

                Depending on your concrete implementation of QAbstractTableModel, it may or may not be an important issue that this extra sort call matters. In my case, since I am using QSqlQueryModel(), the unavoidable sort call does an actual call to a database SELECT, which is "slow" and therefore "bad". If your data is all in-memory, it probably doesn't actually matter that much, but it does to me.

                To resolve, what @m-sue is suggesting is that I (you?) override both QTableView::setSortingEnabled() and QAbstractItemModel::sort() along the lines shown in my pseudo-code above. The effect being that setSortingEnabled() will set a flag to cause sort() to do nothing while it is being called from setSortingEnabled() (which calls sortByColumn() which in turn calls sort()). The approach is "ugly", but seems to be what will work if you want to avoid the implicit sort() caused by setSortingEnabled(), which is what I want and seems to be what you want too.

                M 1 Reply Last reply 10 Nov 2017, 10:18
                1
                • J JonB
                  10 Nov 2017, 08:19

                  @mapuna
                  On the contrary, I am saying: I do understand your issue, and yes it is a problem, for me too! I will try to be brief here, as we have already covered the detail if you read through above responses.

                  It boils down to:

                  • Yes, you do need to call tableView->setSortingEnabled(true) (somewhere) to enable sorting on the table.
                  • No, it is not possible for tableView->setSortingEnabled(true) to just "set a flag" and not call sortByColumn(), it always calls that, unfortunately.

                  Depending on your concrete implementation of QAbstractTableModel, it may or may not be an important issue that this extra sort call matters. In my case, since I am using QSqlQueryModel(), the unavoidable sort call does an actual call to a database SELECT, which is "slow" and therefore "bad". If your data is all in-memory, it probably doesn't actually matter that much, but it does to me.

                  To resolve, what @m-sue is suggesting is that I (you?) override both QTableView::setSortingEnabled() and QAbstractItemModel::sort() along the lines shown in my pseudo-code above. The effect being that setSortingEnabled() will set a flag to cause sort() to do nothing while it is being called from setSortingEnabled() (which calls sortByColumn() which in turn calls sort()). The approach is "ugly", but seems to be what will work if you want to avoid the implicit sort() caused by setSortingEnabled(), which is what I want and seems to be what you want too.

                  M Offline
                  M Offline
                  mapuna
                  wrote on 10 Nov 2017, 10:18 last edited by
                  #20

                  @JNBarchan Thanks a lot for helping me, I just wanted to know I more thing (I ma newbie in QT ).As suggested by you to subcalss the QTableView, following is my question regarding the same:

                  • When I make DerivedTableView(model) : QTableView(model), how can I add this new View in the QTCreator or do you suggest to add this new view programmatic ally or is there any other away to do so.

                  • I have a tableview added in a complex layout in QtCreator and in the code I add models/delegates to it using
                    ui->tableView_name->setModel(myProxy) etc... can I typecast ui->tableView_name at runtime to DerivedTableView is this also possible and then use DerivedTableView to set models/etc

                  • I also could not find some working example where the Custom View is added in code and used if you can provide some reference it would help.

                  Thanks in advance.

                  J 1 Reply Last reply 10 Nov 2017, 11:28
                  0
                  • M mapuna
                    10 Nov 2017, 10:18

                    @JNBarchan Thanks a lot for helping me, I just wanted to know I more thing (I ma newbie in QT ).As suggested by you to subcalss the QTableView, following is my question regarding the same:

                    • When I make DerivedTableView(model) : QTableView(model), how can I add this new View in the QTCreator or do you suggest to add this new view programmatic ally or is there any other away to do so.

                    • I have a tableview added in a complex layout in QtCreator and in the code I add models/delegates to it using
                      ui->tableView_name->setModel(myProxy) etc... can I typecast ui->tableView_name at runtime to DerivedTableView is this also possible and then use DerivedTableView to set models/etc

                    • I also could not find some working example where the Custom View is added in code and used if you can provide some reference it would help.

                    Thanks in advance.

                    J Offline
                    J Offline
                    JonB
                    wrote on 10 Nov 2017, 11:28 last edited by JonB 11 Oct 2017, 11:28
                    #21

                    @mapuna
                    I'm afraid I'm not the right person to ask really:

                    • I don't use QtCreator, so I don't know.

                    • I'm not sure just what you mean. You cannot just dynamically/run-time cast an object to another type/derived type because "you feel like it", when the object was constructed it must actually have been of the type you are casting to. So your QTableView must actually have been created as a DerivedTableView (including if that's done in QtCreator). But then wherever you can use a QTableView you can use your DerivedTableView, e.g. for setModel() call.

                    • No example from me. I don't even use C++, I use Python! :(

                    But I'm sure someone else can clarify the above.....

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 10 Nov 2017, 22:12 last edited by
                      #22

                      Hi,

                      1. http://doc.qt.io/qt-5/designer-using-custom-widgets.html
                      2. You don't need to, just call setModel like you are doing currently
                      3. Do you mean something like:
                      MyView *view = new MyView;
                      ui->layout_name->add(view);
                      

                      ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • V Offline
                        V Offline
                        Vitaliy Sapronenko
                        wrote on 27 Mar 2019, 16:28 last edited by
                        #23

                        You could set default sorting column to -1 by QHeaderView::setSortIndicator like this

                        yourTableView->horizontalHeader()->setSortIndicator(-1, Qt::AscendingOrder);
                        

                        before running

                        yourTableView->setSortingEnabled(true);
                        

                        because QTableView::setSortingEnabled uses QHeaderView::sortIndicatorOrder and QHeaderView::sortIndicatorSection to identify by which column should model be sorted.

                        Note that documentation of QHeaderView::setSortIndicator tells that not all models support setting sort column to -1 and may even crash in this case.

                        1 Reply Last reply
                        1

                        • Login

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