setData in a model derived from QAbstractTableModel
I expect setData() below to store the string somewhere in the model but the call fails. Do I do something wrong or do I misunderstand what setData() is doing. Same for setHeaderdata()
The simplest code that fails to build the model as I expect is a header file .hpp
Code:
#include <QAbstractTableModel>
{
Q_OBJECT
public:
void setSize(int r, int c) {
rows = r;
cols = c;
}
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
= Qt
::DisplayRole) const
private:
int rows, cols;
};
and a source file .cpp
Code:
#include <QtGui>
#include <trial.hpp>
int main(int argc, char* argv[]) {
Model* model = new Model();
model->setSize(8,6);
if( ! model->setHeaderData(0, Qt::Horizontal, *v) )
qDebug() << "Set hdr failed";
if( ! model->setData(mi, *v) )
qDebug() << "Set data failed";
}
Any help most welcome,
Enno
Re: setData in a model derived from QAbstractTableModel
QAbstractTableModel inherits setData() from QAbstractItemModel. From the docs for QAbstractItemModel::setData():
Quote:
Sets the role data for the item at index to value.
Returns true if successful; otherwise returns false.
The dataChanged() signal should be emitted if the data was successfully set.
The base class implementation returns false. This function and data() must be reimplemented for editable models.
Re: setData in a model derived from QAbstractTableModel
The above very indirectly solved my problem I mistakenly thought setData() would store data inside the model.
I now realize that I myself have to define the data structure as member of the model to keep data for cells. Programmatically I fill that data structure.
The view uses data() to get at this data. In the case of a TableModel rowCount() and columnCount() simply return the number of rows and columns of that structure.
Only for an editable model is a setData() function required to update the data for a cell as only I know what structure the data is kept in
The obvious is sometimes difficult to see.