mercredi 7 janvier 2015

Program crashes after defining new role for model

I tried to implement a new model that sorts numerically as described here.


It looks like this:



#ifndef NUMERICMODEL_H
#define NUMERICMODEL_H

#include <QStandardItemModel>

class NumericModel : public QStandardItemModel
{
public:

enum Role {
SortRole=Qt::UserRole
};

NumericModel() {}
~NumericModel() {}

QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const {

switch ( role ) {
case Qt::DisplayRole:
return index.data().toString();
case SortRole:
return index.data().toUInt();
default:
return index.data().toString();
}

}

};

#endif // NUMERICMODEL_H


I am setting the sort role like this:



QSortFilterProxyModel * proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(&m_movesModel);
proxyModel->setSortRole(NumericModel::SortRole);
qDebug() << __LINE__;
ui->tableView_Moves->setModel(proxyModel);qDebug() << __LINE__;
ui->tableView_Moves->resizeColumnsToContents();qDebug() << __LINE__;


However my program crashes in the last line, when calling ui->tableView_Moves->resizeColumnsToContents().


Aucun commentaire:

Enregistrer un commentaire