I am trying to get a simple QAbstractItemModel to show up in a QListView but am struggling a bit. Here is my model implementation:
TestModel::TestModel(QObject *parent) : QAbstractItemModel(parent)
{
}
QVariant TestModel::data(const QModelIndex &index, int role) const
{
if(role == Qt::DisplayRole)
{
return QVariant("FRED");
}
return QVariant();
}
Qt::ItemFlags TestModel::flags(const QModelIndex &index) const
{
return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
}
QVariant TestModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(role == Qt::DisplayRole)
{
return QVariant("BING");
}
return QVariant();
}
QModelIndex TestModel::index(int row, int column, const QModelIndex &parent) const
{
return createIndex(row, column);
}
QModelIndex TestModel::parent(const QModelIndex &index) const
{
return createIndex(0, 0);
}
int TestModel::rowCount(const QModelIndex &parent) const
{
return 1;
}
int TestModel::columnCount(const QModelIndex &parent) const
{
return 1;
}
If I set the model of my QListView to an instance of the above model then nothing shows up. However, if I use a QTableView instead then it is populated as expected.
What do I need to do to get this simple example to work with a QListView?
Thanks, Alan
List model has no parent hierarchy.
Hence, parent
method shall be implemented as,
QModelIndex TestModel::parent(const QModelIndex &index) const
{
return QModelIndex();
}