PDA

View Full Version : QBstractTableMode derived model, not showing in QTableView



high_flyer
22nd March 2010, 20:51
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 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;


I then set my data model so:


QTableView *pTable = new QTableView();
pTable->setModel((QAbstractTableModel *)&m_logFile->dataModel());
pTable->show();


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);
QModelIndex index = m_logFile->dataModel().index(iRow,iCol,QModelIndex());
f2 = m_logFile->dataModel().data(index,Qt::DisplayRole).toFloat();
}


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.

bmhautz
22nd March 2010, 21:41
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 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;


I then set my data model so:


QTableView *pTable = new QTableView();
pTable->setModel((QAbstractTableModel *)&m_logFile->dataModel());
pTable->show();


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);
QModelIndex index = m_logFile->dataModel().index(iRow,iCol,QModelIndex());
f2 = m_logFile->dataModel().data(index,Qt::DisplayRole).toFloat();
}


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.

First off, see if having an actual pointer to your model makes a difference instead of allocating memory for it in the setModel method; i.e.:
QAbstractTableModelsubclass* myModel; //defined in header file as member of possibly QMainWindow
myModel = new QQAbstractTableModelsubclass(...);
pTable->setModel(myModel);

I'm not exactly sure what is happening when you make the call to setModel with your parameters (not seeing the whole code), but it looks suspicious.

Also, another avenue is seeing your data function, which I don't see listed in your header. This is where the table view actually gets the data for your model.

high_flyer
24th March 2010, 08:45
Hi,

thanks for the reply.

First off, see if having an actual pointer to your model makes a difference instead of allocating memory for it in the setModel method; i.e.:
QAbstractTableModelsubclass* myModel; //defined in header file as member of possibly QMainWindow
myModel = new QQAbstractTableModelsubclass(...);
pTable->setModel(myModel);

I am not sure what do you mean.
The call:


pTable->setModel((QAbstractTableModel *)&m_logFile->dataModel());

is not allocating anything.
m_logFile->dataModel() returns a reference to a model which is allocated and lives in m_logFile.
At the time when I am calling pTable->setModel() the model is well allocated and filled with data, so I am not sure what you might have meant there, can you restate maybe?


I'm not exactly sure what is happening when you make the call to setModel with your parameters (not seeing the whole code), but it looks suspicious.
Which setModel() do you mean? my model subclass setModel() or the pTable->setModel()?
And what exactly you find suspicious - so I can check it.


Also, another avenue is seeing your data function, which I don't see listed in your header. This is where the table view actually gets the data for your model.
True.
I forgot to add it here, I will later when I am home again. (don't have the code here).
However, as I said, my data() function is not being called by the QTableView... all though I checked to make sure my virtual implementations signatures are correct, I will have a look again.

Thanks, and if you have more pointers, please share.

bmhautz
24th March 2010, 14:52
I guess what I'm saying is that it is hard to see what you are trying to do with your model. Posting the code will be helpful to evaluate why you are not seeing the model data in the table view. Otherwise, I can only guess why it's not working.

high_flyer
24th March 2010, 15:04
Hi,

I just ran my code on a different machine, and it works perfectly.
No idea why on my machine it doesn't show up.

Oh well.

Thanks any way!