QtableView setSortingEnabled forcing sortByColumn()
-
@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.
wrote on 10 Nov 2017, 11:28 last edited by JonB 11 Oct 2017, 11:28@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);
?
-
wrote on 27 Mar 2019, 16:28 last edited by
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.