Hi,
I managed to avoid using the Model/View framework for a long time, but now I have to.
My problem is, that my model is not showing up in the QTableView.
I have a QAbstractTableMode subclass, in which all the virtual function that have to be implemented are implemented:
//QAbstractTableModel Virtuals
virtual QVariant headerData
(int section, Qt
::Orientation orientation,
int role
) const;
virtual Qt
::ItemFlags flags
( const QModelIndex & index
) const;
//QAbstractTableModel Virtuals
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
virtual bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
virtual bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
virtual bool setData ( const QModelIndex & index, const QVariant & value, int role );
virtual QModelIndex index ( int row, int column, const QModelIndex & parent) const;
virtual QModelIndex parent ( const QModelIndex & index ) const;
virtual Qt::ItemFlags flags ( const QModelIndex & index ) const;
To copy to clipboard, switch view to plain text mode
I then set my data model so:
pTable->show();
QTableView *pTable = new QTableView();
pTable->setModel((QAbstractTableModel *)&m_logFile->dataModel());
pTable->show();
To copy to clipboard, switch view to plain text mode
but the table comes up empty.
I can test that my model contains the correct data with the following code:
//Log file returns a vector of vectors which desctribe a table.
//The heart of my QAbstractTableModel has also the same vector of vectors.
QVector<QVector<float> > data = m_logFile->data();
float f1,f2;
// f1 is the data from the above 'data' vector and f2 is the data from the QAbstractTableModel subclass.
//and they all match. (all the items in their respective row/col.
for(int iCol=0; iCol < data.size(); iCol++){
for(int iRow=0; iRow < data.at(iCol).size(); iRow++){
f1 = data.at(iCol).at(iRow);
f2 = m_logFile->dataModel().data(index,Qt::DisplayRole).toFloat();
}
//Log file returns a vector of vectors which desctribe a table.
//The heart of my QAbstractTableModel has also the same vector of vectors.
QVector<QVector<float> > data = m_logFile->data();
float f1,f2;
// f1 is the data from the above 'data' vector and f2 is the data from the QAbstractTableModel subclass.
//and they all match. (all the items in their respective row/col.
for(int iCol=0; iCol < data.size(); iCol++){
for(int iRow=0; iRow < data.at(iCol).size(); iRow++){
f1 = data.at(iCol).at(iRow);
QModelIndex index = m_logFile->dataModel().index(iRow,iCol,QModelIndex());
f2 = m_logFile->dataModel().data(index,Qt::DisplayRole).toFloat();
}
To copy to clipboard, switch view to plain text mode
What I also find interesting is, that after I set the model, my models headerData() gets called by the QTableView, but data() does not.
Still, even the table headers are empty when it shows up.
My guess is, that I am not doing something which I have to do (which I missed in the docs), or probably, doing something wrong, and would appreciate if you could help me to figure it out.
Maybe if you can point out things that are common mistakes that I can check to see if I am making.
I didn't implement a delegate yet, as I don't want to interact with my Table yet, at the moment I just want to see my data in the table.
Let me know if you need to see implementation of methods.
Thanks in advance.
Bookmarks