PDA

View Full Version : Relative re-sizing of QTableWidget's columns



anwar.qt
22nd May 2013, 16:31
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.

Santosh Reddy
23rd May 2013, 08:22
A rough solution


class TableWidget : public QTableWidget
{
public:
explicit TableWidget(QWidget * parent = 0)
: QTableWidget(parent)
{
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
horizontalHeader()->setStretchLastSection(true);
}

TableWidget(int rows, int columns, QWidget *parent = 0)
: QTableWidget(rows, columns, parent)
{
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
horizontalHeader()->setStretchLastSection(true);
}

protected:
void resizeEvent(QResizeEvent * event)
{
if(model() and model()->columnCount())
for(int column = 0; column < model()->columnCount(); column++)
setColumnWidth(column, event->size().width() / model()->columnCount());
}
};