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).

New Moon
  • 787
  • 6
  • 21
  • 35
  • Is it QTableWidget or QTableView? Do you use model for this view? Is it possible to show the model's code? – vahancho Sep 10 '13 at 07:56
  • 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); – New Moon Sep 10 '13 at 08:06
  • 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. – vahancho Sep 10 '13 at 08:25

2 Answers2

28

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();
Michael Scheper
  • 6,514
  • 7
  • 63
  • 76
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;}");
thuga
  • 12,601
  • 42
  • 52
  • Yes.. that's what i want. But, I just want to know, Is there anyother way of removing the Header section. – New Moon Sep 10 '13 at 08:12
  • @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. – thuga Sep 10 '13 at 08:31
  • @NewMoon if you are not looking for easy ways, you may create your own widget, based on QHeaderView – Dmitry Sazonov Sep 10 '13 at 08:38
  • @thuga: I think NewMoon might want to remove the header altogether, in which case, there is a simpler way than with a style sheet: http://stackoverflow.com/a/37994662/1450294 – Michael Scheper Jun 23 '16 at 14:34
  • @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).* – thuga Jun 29 '16 at 06:33
  • @thuga: Hmmm, yes, you're right. I think this is called not seeing the forest for the trees. ☺ – Michael Scheper Jun 29 '16 at 12:27