[SOLVED]QTableModel crashes on headerData
Unsolved
General and Desktop
-
I am trying to implement a really simple Table Model inheriting from a
QAbstractTableModel
which looks like this#include <QAbstractTableModel> #include <QStringList> class ViewModel : public QAbstractTableModel { Q_OBJECT public: ViewModel(QList<QStringList>* mat, QStringList* cnames, QStringList* rnames, QObject *parent=0); virtual QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const; virtual int rowCount ( const QModelIndex & parent = QModelIndex() ) const; virtual int columnCount ( const QModelIndex & parent = QModelIndex() ) const; virtual QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const; signals: public slots: private: QStringList* colnames; QStringList* rownames; QList<QStringList>* sourceMat; };
Implemented as
ViewModel::ViewModel(QList<QStringList>* mat, QStringList* cnames, QStringList* rnames,QObject *parent) :QAbstractTableModel(parent) { if(mat->count()!=rnames->count() || mat->at(0).count()!=cnames->count()) return; sourceMat = mat; colnames = cnames; rownames = rnames; } int ViewModel::rowCount(const QModelIndex &parent ) const { Q_UNUSED(parent); return sourceMat->count(); //last line is empty } int ViewModel::columnCount(const QModelIndex &parent ) const { Q_UNUSED(parent); return sourceMat->at(0).count(); } QVariant ViewModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role != Qt::DisplayRole) return QVariant(); qDebug() << orientation << " " << section; if (orientation == Qt::Horizontal) return colnames->at(section); else return rownames->at(section); }
I think the problem is in the
headerData
as therownames
andcolnames
are correct the first times the function is called but when the function gets called for a second iteration (I have no idea why) therownames
andcolnames
are not accessible anymore. Also - thedata
function is never reached.The model is called with a single column and 24 rows and attached to a
QTableView
ViewModel* mod_meta = new ViewModel(mat_meta,&cnamMet,data.rnames); QTableView* tview = new QTableView; tview->setModel(mod_meta); QGridLayout* gl = new QGridLayout; gl->addWidget(tview,0,0); delete frame->layout(); // delete previous layout frame->setLayout(gl);