PDA

View Full Version : QTableView header invisible



smacchia
16th October 2009, 20:23
I'm using a table view with a custom table model derived from QAbstractTableModel. The table's vertical header should be hidden, but the horizontal one should be showing.

My table model (MyModel) implements ::headerData returning a simple QString variant for the header.

It is getting called, but the header won't show up. Any idea why?

I'm creating the view in the following way:



QTableView *view = new QTableView;

view->setSortingEnabled(true);
view->setSelectionBehavior(QAbstractItemView::SelectRows );
view->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
view->setSelectionMode(QAbstractItemView::SingleSelectio n);
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
view->setShowGrid(false);
view->setModel (new MyModel(this));
view->setCurrentIndex(QModelIndex());
view->horizontalHeader()->setStretchLastSection(true);
view->verticalHeader()->hide();

Thanks,
Susan

caduel
17th October 2009, 10:20
well, without your code... just a guess. Do you check roles in headerData()?

squidge
17th October 2009, 17:02
I assume your setting columnCount? Heres a snippet from my model that works:



int TableDetailModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 2;
}

QVariant TableDetailModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();

if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("Id");

case 1:
return tr("Description");

default:
return "Unknown!";
}
}
return QVariant();
}

smacchia
19th October 2009, 16:02
Yes I had ::columnCount and ::headerData. But, I was missing the following from ::headerData


if (role != Qt::DisplayRole)
return QVariant();


Silly mistake on my part. But you're reply helped me figure out what was missing. The headers are showing up with this snippet in place :)

thanks!

rockballad
5th November 2009, 04:03
Hi, thanks for your post. I have the same situation. But I wanna hide the column header. I tried many ways, but there's still an empty header (row 0). I can remove all headers or just row header (column 0) like you do. Could you help me out? Thanks in advance.

rockballad
17th November 2009, 11:25
Hi, could anybody please help me create a table without row-header (row 0). It'd look like this:

Row1|data-1|data-2
Row2|data-3|data-3

data-x is editable while Row1/row2 is not (it's header-like). I tried many ways but still not able to do so.

Thanks.

squidge
17th November 2009, 11:34
Have you tried view->horizontalHeader()->hide() or is that not what your looking for?

rockballad
20th November 2009, 04:21
Have you tried view->horizontalHeader()->hide() or is that not what your looking for?

Great! It's exactly what I'm looking for! Thank you so much!