View Full Version : QHeaderView and its size

8th October 2009, 17:51

I'm using a QStandardItemModel with QStandardItems to fill a QTableView. (Qt 4.5.2 on Debian Linux)

QTableView *table = new QTableView();
QStandardItemModel *model = new QStandardItemModel(num_rows, num_cols);

//add a header for column 0
QStandardItem *header_item = new QStandardItem("Some Title");
model->setHorizontalHeaderItem(0, header_item);

//add more headers and items to the model.....

//now let the columns resize to fit their contents


The problem occurs when I set the size of the columns to stretch automatically to its contents, but then the content of the headers itself does not fit.
It seems only the contents of the actual data rows are used for stretching.

How can I stretch the horizontal headers in a way that it will also fit the text inside the headers?
Note that the text inside the headers are displayed in a slightly larger and bold font.

The same things happen when I use: table->resizeColumnToContents(0);

Thank you in advance

9th October 2009, 09:26
Problem spotted... ;-)

The header sections in my previous post are styled with qss, it all works fine, however....

QHeaderView::section {
background-color: white;
color: black;
font: bold 12px;
border: 1px solid black;
/* height: 20px; */

First, I had set the height property for a header section, but somehow it caused the wrong calculations for stretching the sections correctly.

Is this expected behavior? Or might it be a bug?