Implementing QAbstractTableModel
-
Well, I am not sure if i understand you. I am using QList< QList<QString> > and i am displaying QString. The thing is, that data method gets called just once with index 0,0 and value stored in QList on this index is displayed just fine, but others are not, since data method is not called anymore...
ie:
QList< QList<QString> > my_data is filled with following
1 2
3 4
In main.qml i got following:
@
MyModel{
id: varModel
dataId: "matrix"
}GridView{
id: grid
model: varModel
delegate: {
Text{
text: value
}
}
}
@Method data gets called just once with index 0,0 and value 1 which is on this index is displayed fine, others are not...
-
I really don't like your design at all. Please considder "my docnote":http://developer.qt.nokia.com/doc/qt-4.7/model-view-programming.html#note-13 on designing your own QAbstractItemModels.
I think you first need to define a data store object that handles your blob of data, and supplies a sane API to query it. Only then, you create a QAbstractItemModel (or QAbstractTableModel, in your case) that works on top of that data store. The model is only used as an interface between the Qt item views and your internal data store. It is not suitable as a general data store, I think. The data itself should not be contained within the model.
-
Here is link to my project on dropbox: "Test":http://dl.dropbox.com/u/44811245/Test.tar.gz
It reads data from text file specified in constructor of DeviceControll class. Data in file should be in following format: "dataId : rowCount colCount values", values should be separated by space. ie: "myMatrix: 2 2 1 2 3 4".Andre, thanks for your reply and link to your docnote. It explained a lot to me, now i get how model should be used and next time i will stick to that!;-)
This "project" is just my playground and I'v followed example that I'v linked above.
-
hi portoist,
I found one point:@
QVariant myTable::data(const QModelIndex &index, int role) const
{
if (role != valueRole){
return QVariant();
}
...
}
@this should not be, as you return for each role other than value role.
Try this code for the model data function:@
QVariant myTable::data(const QModelIndex &index, int role) const
{
if(index.isValid())
{
cout<<"Data called with column:"<<index.column()<<" row:"<<index.row()<<endl;
if(Qt::DisplayRole == role)
{
if (this->m_data.count() <= 0){
return QVariant();
}
QList<QString> tmpLists = this->m_data[index.row()];
QString s = tmpLists[index.column()];
cout << "Data will return: "<<s.toLocal8Bit().data()<<endl;
return s;
}
}
return QAbstractTableModel::data(index, role);
}
@ -
[quote author="Andre" date="1318239693"]Indeed, it is pure virtual (normal virtual methods would work, of course). I guess this was just a matter of habbit from Gerolf to call base implementations as the default case; and a good habbit that is too! :-)[/quote]
aeh, yep, I did not test it otherwise I would have seen that :-) sorry.
a return of an empty variant should be it. -
Well, it still doesn't solve my problem :-) How can I tell to the view that data are invalid? When I am using just simple List, I just call beginInsertRows and endInsertRows. It works ok and data method is requested for row 0,1,2.
I have tried calling beginResetModel each time before values are set but all it does is that it calls data method once with index of column 0 and row 0.
I gues I will have to use just simple list of QString and return whole rows in data method...