PDA

View Full Version : model/view, can't hide row/col



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?

Jimmy2775
14th September 2007, 18:35
If you're trying to hide the horizontal header, you might want to take this approach:


view->horizontalHeader()->hide();

jpn
14th September 2007, 19:53
Perhaps it's because by the time you hide the row there is nothing in the model. You insert (init()) items afterwards.

grellsworth
15th September 2007, 17:13
If you're trying to hide the horizontal header, you might want to take this approach:


view->horizontalHeader()->hide();

I don't want to hide my horizontal header, but I want to hide row 0, which contains my header data.

grellsworth
15th September 2007, 17:14
Perhaps it's because by the time you hide the row there is nothing in the model. You insert (init()) items afterwards.

I thought that might be the case, so I moved things around so that the init was first and the hide*() functions were after it, but it didn't make any difference.

jpn
15th September 2007, 17:15
May we see TableModel::TableModel() and TableModel::init()?

grellsworth
17th September 2007, 13:20
As requested:


TableModel::TableModel(QObject *parent)
: QAbstractTableModel(parent)
{
init();
}

//--//--//
void TableModel::init()
{
// Set my header data in element 0 of my stringlist.
column.insert(0, QStringList("Col A"));
column.insert(1, QStringList("Col B"));
column.insert(2, QStringList("Col C"));

// set row 1 data in column 1, 2 and 3
column[0].append("greco");
column[1].append("dan");
column[2].append("gordon");

// set row 2 data in column 1, 2 and 3
column[0].append("trish");
column[1].append("jessie");
column[2].append("morgan");
}

I've attached a picture (screenshot) of what this looks like... notice row 0 has the same data as my header... now if I could just hide row 0, everything would be fine!!!

Sincerely,

Gordon E.

jpn
17th September 2007, 14:21
Apparently headerData() returns column[N].at(0) so couldn't you make data() return data starting from column[N].at(1), and make rowCount() return column[N].count() - 1?