Unsolved QAbstractItemModel::sort() - disable after executing once
-
I have been using this convenient function to sort columns of a table in ascending/descending order, whenever a horizontal header is clicked in my QTableView. However, setting the sort column will lead to constant updates of the models sort order whenever data is added or modified.
I am working with a pretty big table, and it is not a desirable behavior to have rows disappear in "scroll heaven" after changing values in the sort column.
I tried setting the sort column to -1 after sorting once, but that reset my order to the initial row indexes.
What is the quickest fix to simply update the sort order once, every time the header is clicked? Do I have to subclass from QSortFilterProxyModel? If so, which function would I have to override?
Here is some example code in case you need to replicate what I was describing:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , table_view_(0) { table_view_ = new QTableView(this); QStandardItemModel* model = new QStandardItemModel(this); model->setColumnCount(3); model->setRowCount(10); model->setHeaderData(0, Qt::Horizontal, QString("Header One")); model->setHeaderData(1, Qt::Horizontal, QString("Header Two")); model->setHeaderData(2, Qt::Horizontal, QString("Header Three")); for(int row = 0; row < model->rowCount(); ++row) { int x,y,z; x = qrand() % 100; y = qrand() % 100; z = qrand() % 100; model->setData(model->index(row, 0), QString::number(x)); model->setData(model->index(row, 1), QString::number(y)); model->setData(model->index(row, 2), QString::number(z)); } QSortFilterProxyModel* sort_model = new QSortFilterProxyModel(this); sort_model->setSourceModel(model); table_view_->setModel(sort_model); table_view_->horizontalHeader()->setSectionsClickable(true); connect(table_view_->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(onHorizontalHeaderSectionClicked(int))); this->setCentralWidget(table_view_); } void MainWindow::onHorizontalHeaderSectionClicked(int section) { table_view_->model()->sort(section); }
-
never mind, QSortFilterProxyModel::setDynamicSortFilter(false) does the trick.