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. QAbstractItemModel::sort() - disable after executing once
Forum Updated to NodeBB v4.3 + New Features

QAbstractItemModel::sort() - disable after executing once

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 2.7k 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.
  • VagabondV Offline
    VagabondV Offline
    Vagabond
    wrote on last edited by
    #1

    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);
    }
    
    1 Reply Last reply
    0
    • VagabondV Offline
      VagabondV Offline
      Vagabond
      wrote on last edited by
      #2

      never mind, QSortFilterProxyModel::setDynamicSortFilter(false) does the trick.

      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