PDA

View Full Version : Table Model / View Problem -- Data Not Displaying



jhendersen
22nd April 2007, 05:14
Hello everyone,

I'm having a difficult time getting a table set up. I subclassed QAbstractTableModel as such:


int FileModel::rowCount(const QModelIndex &parent) const {

return files.count();
}
int FileModel::columnCount(const QModelIndex &parent) const {
return 4;
}
QVariant FileModel::data(const QModelIndex &index, int role) const {
printf("row %d\n",(int)index.row());
if (!index.isValid())
return QVariant();

if (index.row() >= files.size())
return QVariant();

if (role == Qt::DisplayRole) {
printf("%s\n",files.value(file_list.at(index.row()))->m_path.c_str());
return QString(files.value(file_list.at(index.row()))->m_path.c_str());
} else {
return QVariant();
}
}


I set the model in a view like this:

FileModel fmodel;
QTableView *table = new QTableView(this);
table->setModel(&fmodel);
table->setAlternatingRowColors(true);
table->show();

However, an empty table shows up with the correct number of rows and columns. I'm not sure why this is happening and I've tried just about everything I can think of.

Any ideas?

Thanks!

jpn
22nd April 2007, 06:45
The model gets automatically destroyed when going out of scope. Allocate it on the heap:


FileModel* fmodel = new FileModel(this);
QTableView *table = new QTableView(this);
table->setModel(fmodel);

PS. Have you noticed QDirModel? :)