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:
Any idea how do I do it? I guess most of the answers concerned Qt 4, I am using Qt 5.