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, which view are you using?
-
Did you try first to validate your model functionality with a QTableView ?
-
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.