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. Sorting with QStandardItemModel and QTableView

Sorting with QStandardItemModel and QTableView

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 6.8k Views 1 Watching
  • 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.
  • Z Offline
    Z Offline
    zarnick
    wrote on last edited by
    #1

    Hello, I'm trying to make a simple sorting on a QTableView, where the model is a QStandardItemModel, and the sorting is a QSortFilterProxyModel, but a very strange thing is going on.
    What I need is automatic sorting, which is, every time a object is modified on the model, it should sort acordingly to the column number 7. It works, partially, if I use the table->sortByColumn(7,Qt::Descending), it works normally, however for Qt::Acending, it starts changing the data on the last row, and only that!
    And the more strange part is, if I use manuall sorting and click on the header, it works flawlessly.
    Here are some snippets of the code I'm using:
    This is the constructor:
    @
    ui->setupUi(this);
    this->connectSlots();

    //Create the model
    this->model = new QStandardItemModel(ui->athleteView);
    this->refreshModel();
    
    this->proxyModel = new QSortFilterProxyModel(this);
    this->proxyModel->setSourceModel(model);
    ui->athleteView->setModel(this->proxyModel);
    ui->athleteView->setSortingEnabled(true);
    ui->athleteView->sortByColumn(7);
    

    @

    And this the method that is called every time there's a change on the model:
    @
    void InlineSpeedTimer::refreshModel(){
    this->model->clear();
    int row, col;
    row=0;
    //Set headers
    col=0;
    this->model->setHorizontalHeaderItem(col++,new QStandardItem("Name"));
    this->model->setHorizontalHeaderItem(col++,new QStandardItem("Surname"));
    this->model->setHorizontalHeaderItem(col++,new QStandardItem("Team"));
    this->model->setHorizontalHeaderItem(col++,new QStandardItem("Age"));
    this->model->setHorizontalHeaderItem(col++,new QStandardItem("Time 1"));
    this->model->setHorizontalHeaderItem(col++,new QStandardItem("Time 2"));
    this->model->setHorizontalHeaderItem(col++,new QStandardItem("Time 3"));
    this->model->setHorizontalHeaderItem(col++,new QStandardItem("Final time"));
    //Set information
    for(athlItr=this->athletes.begin(); athlItr!=this->athletes.end(); ++athlItr){
    col=0;
    this->model->setItem(row,col++, new QStandardItem((*athlItr)->getName()) );
    this->model->setItem(row,col++, new QStandardItem((*athlItr)->getSurname()) );
    this->model->setItem(row,col++, new QStandardItem((*athlItr)->getTeam()) );
    this->model->setItem(row,col++, new QStandardItem( QString::number((*athlItr)->getAge())) );
    this->model->setItem(row,col++, new QStandardItem( this->createTimeFromMsecs( (*athlItr)->getTime(0)) ));
    this->model->setItem(row,col++, new QStandardItem( this->createTimeFromMsecs( (*athlItr)->getTime(1)) ));
    this->model->setItem(row,col++, new QStandardItem( this->createTimeFromMsecs( (*athlItr)->getTime(2)) ));
    this->model->setItem(row,col++, new QStandardItem( this->createTimeFromMsecs(
    ( (*athlItr)->getTime(0)+(*athlItr)->getTime(1)+(*athlItr)->getTime(2) )/3)));
    for(col=0;col<8;col++)
    this->model->item(row,col)->setEditable(false);

        row++;
    }
    if(this->athletes.size()>0){
        this->ui->editButton->setEnabled(true);
        this->ui->deleteButton->setEnabled(true);
    }
    else{
        this->ui->editButton->setEnabled(false);
        this->ui->deleteButton->setEnabled(false);
        this->ui->startButton->setEnabled(false);
    }
    this->ui->athleteView->resizeColumnToContents(4);
    this->ui->athleteView->resizeColumnToContents(5);
    this->ui->athleteView->resizeColumnToContents(6);
    this->ui->athleteView->resizeColumnToContents(7);
    this->ui->athleteView->sortByColumn(7/*,Qt::AscendingOrder*/);
    

    }
    @
    If you see the call this->ui->athleteView->sortByColumn(7/,Qt::AscendingOrder/);, if I remove the comments regarding the Qt::AscendingOrder, it cracks everything on the table view as I've told before.

    Thanks for the help.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bjanuario
      wrote on last edited by
      #2

      have you used setClickable(true) on your QHeaderView?

      Take a look also on this code:
      @void QTableView::sortByColumn(int column)
      {
      Q_D(QTableView);

      if (!d->model)
      return;
      bool ascending = (horizontalHeader()->sortIndicatorSection() == column
      && horizontalHeader()->sortIndicatorOrder() == Qt:escendingOrder);
      Qt::SortOrder order = ascending ? Qt::AscendingOrder : Qt:escendingOrder;
      horizontalHeader()->setSortIndicator(column, order);
      d->model->sort(column, order); // <-- SORT IS HERE
      }@

      Hope this helps,
      Kind Regards

      1 Reply Last reply
      0

      • Login

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