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