How to Remove the Header in QTableView?
Asked Answered
P

2

8

As shown in the Image below, How can i remove the unwanted header section ?

QTableView with 4 columns

My Table has to display only 4 column headers. It should not display the whole header section. Please Help to remove the Header Section which is displaying after the 4th column (Header section which is highlighted).

Philous answered 10/9, 2013 at 7:50 Comment(3)
Is it QTableWidget or QTableView? Do you use model for this view? Is it possible to show the model's code?Lashay
It is QTableView. Here's my Model's code. myStandardItemModel = new QStandardItemModel(0,4,this); myStandardItemModel->setHorizontalHeaderItem(0, new QStandardItem(QString("1"))); myStandardItemModel->setHorizontalHeaderItem(1, new QStandardItem(QString("2"))); myStandardItemModel->setHorizontalHeaderItem(2, new QStandardItem(QString("3"))); myStandardItemModel->setHorizontalHeaderItem(3, new QStandardItem(QString("4"))); table->setModel(myStandardItemModel);Philous
thanks. Hm, I investigated more on this, but didn't find any better solution than thuga has suggested, i.e. stratching the last section. I also think that hiding the last section in the way you want is even not possible. I saw such tables in Qt3, but never in latter implementations of Qt.Lashay
G
29

From your comments in the other answer, I wonder if, by 'it should not display the whole header section', you mean you want to remove the header altogether.

If so, here's how:

myTable->horizontalHeader()->hide();
Gratis answered 23/6, 2016 at 14:33 Comment(1)
Requires #include <QHeaderView>Creationism
W
6

You can stretch the last column to take all the avaiable space using the stretchLastSection property:

myTable->horizontalHeader()->setStretchLastSection(true);

Or you can hide it with a stylesheet:

myTable->setStyleSheet("QHeaderView {background-color: transparent;}");
Waldheim answered 10/9, 2013 at 7:58 Comment(6)
Yes.. that's what i want. But, I just want to know, Is there anyother way of removing the Header section.Philous
@NewMoon I don't think there is anything that is simpler than hiding it with a style sheet. You'd probably have to resize the header so it's the same width as the total width of your columns. If your columns are set to fixed width, then it is fairly simple, but I'd still go with the style sheets.Waldheim
@NewMoon if you are not looking for easy ways, you may create your own widget, based on QHeaderViewBombproof
@thuga: I think NewMoon might want to remove the header altogether, in which case, there is a simpler way than with a style sheet: https://mcmap.net/q/1239644/-how-to-remove-the-header-in-qtableviewGratis
@MichaelScheper No. In his question he highlighted only the stretched section, and he stated Please Help to remove the Header Section which is displaying after the 4th column (Header section which is highlighted).Waldheim
@thuga: Hmmm, yes, you're right. I think this is called not seeing the forest for the trees. ☺Gratis

© 2022 - 2024 — McMap. All rights reserved.