QAbstractTableModel columns always zero
-
Hi,
I'm Using aQAbstractTableModel, my row is a
@QList< QPair<QString, QString> > listOfPairs;@And I set my listOfPairs at:
@void MyTableModel::setCurrencyMap(const QList< QPair<QString, QString> >&map)
{
listOfPairs = map;
reset();
}@The problem is that at data() method my index.column() always is zero.
Thanks
-
Hi,
Are you sure you reimplemented columnCount correctly ?
-
Hi SGaist
This is my columnCount
@int MyTableModel::columnCount(const QModelIndex &/parent/) const
{
return 2;
}@Thanks for your quick response
-
Hi, which view are you using?
-
Hi 2,
I'm exporting my model to QML, I have proved with both GridView and ListView, but both have index.colum() = 0.
Thanks
-
Did you try first to validate your model functionality with a QTableView ?
-
Not Sgaist,
this model is for use it at QML, I've used with GridView and ListView both with the same result. index.column() = 0
-
I understood that, I just proposed that you first ensure that your model works properly from a C++ point of view before going with QML, that would allow you to eliminate some errors
-
I'm sorry SGaist...
I've wasted a lot of time in that part so I've 'resolved' it asking for roles instead of columns like this:
@
QVariant MyModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();if(index.row() >= listOfPairs.size() || index.row() < 0)
return QVariant();if(role == Qt::DisplayRole || role == MyRole1 || role == MyRole2) {
QPair<QString, QString> pair = listOfPairs.at(index.row());if(role == MyRole1) return pair.first; else if(role == MyRole2){ return pair.second; }
}
return QVariant();
}@thanks
-
Look for modeltest. It is a class that you can instantiate with your model, and it will tell you all the things that are wrong with it. Your implementation of columnCount is wrong, for instance.
Also note that QML doesn't really deal with columns at all. QML works with lists, not grids and not trees.
-
Thanks Andre, That sounds very useful.