Home:ALL Converter>Qt table last column not stretching to fill in parent

Qt table last column not stretching to fill in parent

Ask Time:2014-11-13T18:30:53         Author:SexyBeast

Json Formatter

I am trying to use a QTableWidget (I also tried a QTableView) to tabulate some data. I expected the last column to stretch and fill the parent, but it isn't. I found a couple of answer on Stackoverflow which asked me to do the following:

QTableWidget* table = new QTableWidget();
QHeaderView* headerView = new QHeaderView(Qt::Horizontal);
table->setWindowTitle(QString::fromUtf8("QTableWidget Header Stretch"));
table->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
table->setColumnCount(2);
table->setRowCount(1);
//Set Header Label Texts Here
table->setHorizontalHeaderLabels(QString("HEADER 1;HEADER 2").split(";"));
table->setItem(0,0,new QTableWidgetItem("CELL 1"));
table->setItem(0,1,new QTableWidgetItem("CELL 2"));
headerView->sectionResizeMode(QHeaderView::Stretch);
//add the stretch here
headerView->stretchLastSection();
table->setHorizontalHeader(headerView);
table->show();

However, it is still not stretching to fill, as evident from the following picture:

enter image description here

Any idea how do I do it? I guess most of the answers concerned Qt 4, I am using Qt 5.

Author:SexyBeast,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/26906708/qt-table-last-column-not-stretching-to-fill-in-parent
yy