Relative re-sizing of QTableWidget's columns

740 Views Asked by At

I am using a QTableWidget with 4-5 columns in a dialog. The dialog is resizable, I want table widget columns to resize according to dialog size i.e. if I increase dialog width, columns which are initially set with large width should expand more than the columns which were set with less width.

In short, I want relative resizing like column1 should occupy 20%, column2 occupy 50% of my table width (which increases with dialog width) and so on.

How this can be achieved for QTableWidget in Qt ?

Any solution, pointers or hints would be very helpful.

1

There are 1 best solutions below

0
On

It should just be a matter of updating the column widths whenever your dialog resizes.

MyDialog::resizeEvent(QResizeEvent *event) {
  int width = ui->tableWidget->size().width();
  ui->tableWidget->setColumnWidth(0, width * .2);
  ui->tableWidget->setColumnWidth(1, width * .5);
  ...
}

You could also subclass QTableWidget directly and do this same thing.