I had a very similar issue with QAbstractListModel in C++ (the code I tried was the same (but in C++)) I found a solution by looking at the source of QStringListModel.
my working C++ test class looks like:
{
public:
{
// we are a list, so if there is a parent return 0 (see the Qt source of QStringListModel)
if (parent.isValid())
return 0;
return 4; // we have 4 rows
}
{
// if an invalid row, return empty QVariant
if (index.row() < 0 || index.row() >= 4)
switch( role )
{
case Qt::DisplayRole:
case Qt::EditRole:
{
s
= QString("row [%1]").
arg( index.
row() );
}
default:
}
}
};
class MyModel : public QAbstractListModel
{
public:
MyModel( QObject * parent ) : QAbstractListModel(parent) {}
int rowCount( const QModelIndex & parent ) const
{
// we are a list, so if there is a parent return 0 (see the Qt source of QStringListModel)
if (parent.isValid())
return 0;
return 4; // we have 4 rows
}
QVariant data( const QModelIndex & index, int role /* = Qt::DisplayRole*/ ) const
{
// if an invalid row, return empty QVariant
if (index.row() < 0 || index.row() >= 4)
return QVariant();
switch( role )
{
case Qt::DisplayRole:
case Qt::EditRole:
{
QString s;
s = QString("row [%1]").arg( index.row() );
return QVariant(s);
}
default:
return QVariant();
}
}
};
To copy to clipboard, switch view to plain text mode
I believe that it is the 0 returned if the parent is valid from `rowCount` is what you need to do, if I just returned 4 in all cases I get no list displayed.
Hope this solves your issue.
Bookmarks