grellsworth
14th September 2007, 18:18
I made my own custom model to represent a table.
I used a QList<QStringList>, each element of the QList represents a column of my table and each element of my QStringList represents a row (with row 0 being my header data).
I've followed the model/view programming documentation from the Trolltech website, but my view will not hide columns or rows for some reason.
I'm assuming that the view is populating it's information by using my model's data() function.
Here's my model's data() function:
QVariant TableModel::data(const QModelIndex &index, int role) const
{
int rows = 0;
if(!index.isValid())
{
return QVariant();
}
// NOTE: I do this to prevent "invalid index" errors, if my QList is empty.
if(column.empty())
{
rows = 0;
}
else
{
rows = column[0].count();
}
if((index.row() >= rows) ||
(index.column() >= column.size()))
{
return QVariant();
}
if(role == Qt::DisplayRole)
{
return column[index.column()].at(index.row());
}
else
{
return QVariant();
}
}
And in my main() function:
int main(int argc, char **argv)
{
QApplication myApp(argc, argv);
TableModel data;
QAbstractItemModel *model = &data;
QTableView *view = new QTableView;
view->setModel(model);
view->setGridStyle(Qt::NoPen);
// Trying to hide row 0, which contains my header data.
view->hideRow(0);
view->setRowHidden(0, true);
// neither of these functions hide anything.
view->show();
// This is just a test function that fills the table with data.
data.init();
return myApp.exec();
}
I've tried hideColumn() also, and it does nothing. I've also tried other numbers (1,2,3, etc) for hideRow() and hideColumn(), but nothing happens.
What am I doing wrong?
I used a QList<QStringList>, each element of the QList represents a column of my table and each element of my QStringList represents a row (with row 0 being my header data).
I've followed the model/view programming documentation from the Trolltech website, but my view will not hide columns or rows for some reason.
I'm assuming that the view is populating it's information by using my model's data() function.
Here's my model's data() function:
QVariant TableModel::data(const QModelIndex &index, int role) const
{
int rows = 0;
if(!index.isValid())
{
return QVariant();
}
// NOTE: I do this to prevent "invalid index" errors, if my QList is empty.
if(column.empty())
{
rows = 0;
}
else
{
rows = column[0].count();
}
if((index.row() >= rows) ||
(index.column() >= column.size()))
{
return QVariant();
}
if(role == Qt::DisplayRole)
{
return column[index.column()].at(index.row());
}
else
{
return QVariant();
}
}
And in my main() function:
int main(int argc, char **argv)
{
QApplication myApp(argc, argv);
TableModel data;
QAbstractItemModel *model = &data;
QTableView *view = new QTableView;
view->setModel(model);
view->setGridStyle(Qt::NoPen);
// Trying to hide row 0, which contains my header data.
view->hideRow(0);
view->setRowHidden(0, true);
// neither of these functions hide anything.
view->show();
// This is just a test function that fills the table with data.
data.init();
return myApp.exec();
}
I've tried hideColumn() also, and it does nothing. I've also tried other numbers (1,2,3, etc) for hideRow() and hideColumn(), but nothing happens.
What am I doing wrong?