QtableView setSortingEnabled forcing sortByColumn()
-
@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.
-
@mapuna
I don't see you are doing anything "wrong". The whole point of the discussion above is that you do need to calltableView->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.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.
-
Hi,
Maybe QSortFilterProxyModel might be of interest ?
-
@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
-
@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 callsortByColumn()
, 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 usingQSqlQueryModel()
, the unavoidable sort call does an actual call to a databaseSELECT
, 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()
andQAbstractItemModel::sort()
along the lines shown in my pseudo-code above. The effect being thatsetSortingEnabled()
will set a flag to causesort()
to do nothing while it is being called fromsetSortingEnabled()
(which callssortByColumn()
which in turn callssort()
). The approach is "ugly", but seems to be what will work if you want to avoid the implicitsort()
caused bysetSortingEnabled()
, which is what I want and seems to be what you want too. - Yes, you do need to call
-
@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.
-
-
@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 aDerivedTableView
(including if that's done in QtCreator). But then wherever you can use aQTableView
you can use yourDerivedTableView
, e.g. forsetModel()
call. -
No example from me. I don't even use C++, I use Python! :(
But I'm sure someone else can clarify the above.....
-
-
Hi,
- http://doc.qt.io/qt-5/designer-using-custom-widgets.html
- You don't need to, just call
setModel
like you are doing currently - Do you mean something like:
MyView *view = new MyView; ui->layout_name->add(view);
?
-
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.