QBstractTableMode derived model, not showing in QTableView
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:
Code:
//QAbstractTableModel Virtuals
virtual QVariant headerData
(int section, Qt
::Orientation orientation,
int role
) const;
virtual Qt
::ItemFlags flags
( const QModelIndex & index
) const;
I then set my data model so:
but the table comes up empty.
I can test that my model contains the correct data with the following code:
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();
}
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.
Re: QBstractTableMode derived model, not showing in QTableView
Quote:
Originally Posted by
high_flyer
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:
Code:
//QAbstractTableModel Virtuals
virtual QVariant headerData
(int section, Qt
::Orientation orientation,
int role
) const;
virtual Qt
::ItemFlags flags
( const QModelIndex & index
) const;
I then set my data model so:
but the table comes up empty.
I can test that my model contains the correct data with the following code:
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();
}
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.
Re: QBstractTableMode derived model, not showing in QTableView
Hi,
thanks for the reply.
Quote:
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:
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?
Quote:
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.
Quote:
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.
Re: QBstractTableMode derived model, not showing in QTableView
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.
Re: QBstractTableMode derived model, not showing in QTableView
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!