PDA

View Full Version : How to set the QTableWidget's columns width to fit the widget



rawfool
3rd June 2013, 14:45
I'm using a QTableWidget, but I'm looking for a way to dynamically resize the columns to fit the widget base width.

I tried this, but it's resizing the column contents only to fit it's column. But I need the columns resize itself to the widget's width.

for(int nCol, nRow = 0; nRow < 3; nRow++)
{
for(nCol = 0; nCol < 4; nCol++)
{
vulnTable->setItem(nRow, nCol, new QTableWidgetItem(QString::number(nRow) + " " + QString::number(nCol)));
vulnTable->resizeRowsToContents();
vulnTable->resizeColumnsToContents();
}
}

And the result is as shown in the pic. How do I avoid the gap left-out?
9097

Thank you.

Santosh Reddy
3rd June 2013, 16:25
Use this


QTableWidget::horizontalHeader().setStretchLastSec tion(true);

pradeepreddyg95
4th June 2013, 03:46
use this
ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);

rawfool
4th June 2013, 05:52
For QTableWidget::horizontalHeader().setStretchLastSec tion(true);

error: C2228: left of '.setStretchLastSection' must have class/struct/union
type is 'QHeaderView *'
did you intend to use '->' instead?

And if I try this - tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);

error: C2027: use of undefined type 'QHeaderView'
error: C2065: 'Stretch' : undeclared identifier

NB: It's not even giving suggestions after horizontalHeader(). I'm using Qt 5.0.2

rawfool
5th June 2013, 06:13
QTableWidget::horizontalHeader().setStretchLastSec tion(true);
Worked. Thanks

kantEmbedded
5th September 2014, 08:05
use this
ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);

Hi... you suggestion was useful

wissam.h.elkadi@gmail.com
15th September 2015, 11:18
I have another problem after doing this, header then cut like the this image 11373

the second column in the first table is [Manufacturer]
the second column header is [Apical Diameter] and the third column is [Occlusal Diameter]

how can i make the table fit width of both header and content and also the columns resize itself to the widget's width.

thanks