[Interest] sizing QTableWidget columns

Waitman Gobble waitman at waitman.net
Tue Jul 18 00:34:24 CEST 2017



On 2017-07-17 17:10, Waitman Gobble wrote:

> I want QTableWidget columns to fit the data and also the far right 
> column expand to use all available space.
> 
> {
> ui->vendorsGrid->clear();
> ui->vendorsGrid->setRowCount(0);
> ui->vendorsGrid->setSelectionBehavior(QAbstractItemView::SelectRows);
> ui->vendorsGrid->setSelectionMode(QAbstractItemView::SingleSelection);
> ui->vendorsGrid->verticalHeader()->hide();
> 
> ui->vendorsGrid->setColumnCount(7);
> ui->vendorsGrid->setSortingEnabled(true);
> ui->vendorsGrid->setHorizontalHeaderLabels(QString("idx;Active;Created;City;State;Country;Name").split(";"));
> 
> //load data
> 
> ui->vendorsGrid->resizeColumnsToContents();
> ui->vendorsGrid->horizontalHeader()->sectionResizeMode(QHeaderView::Stretch);
> ui->vendorsGrid->horizontalHeader()->setStretchLastSection(true);
> ui->vendorsGrid->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
> 
> }
> 
> This works on the first data load, but if I subsequently 'reload' the 
> data, the right column does adjust to fit the data.  However it does 
> not expand to use the available space, until I either click a column 
> header ie 'sort', or resize the main window. Only then the right column 
> expands to fill the available space. I've tried adding:
> 
> this->resize(this->width(),this->height());
> 
> to force a resize but does not remedy the behavior.
> 
> I've tried changing the order of 
> resizeColumnsToContents/sectionResizeMode/setSizePolicy to no avail.
> 
> I'm using Qt version 5.7.1 on FreeBSD 11.0-RELEASE-p9 #0
> 
> I appreciate help/suggestions.



Kind of hokey fix but this seems to solve the issue:

     this->resize(this->width()-10,this->height()-10);
     this->resize(this->width()+10,this->height()+10);




---
Waitman Gobble
+52 352 132 37 72



More information about the Interest mailing list