PDA

View Full Version : auto resizing column/row widths



raman_31181
11th September 2008, 12:29
Is there any way to force the table to autoresize its column widths according to no. of columns.I have an embedded app. here where I am displaying data in a tabular format but the no. of colums changes run time.I want that the columns/rows should automatically resize themselves run time(within a fixed area) but without adding vertical or horizontal scroll bars since I dont have mouse in my application.I know this sounds difficult but one feels very optimistic with Qt around :)

Following is my code right now


void VCConfig::createCalTable()
{
for(int i=0;i<10;i++)
{
calzvalues << &calzvalue[i];

calsvalues << &calsvalue[i];
}
QBrush valuebackground( Qt::yellow, Qt::SolidPattern );
QBrush labelbackground( Qt::cyan, Qt::SolidPattern );

calTable = new QTableWidget( 3, 11, this );
calTable->setColumnWidth (0,42);
for(int i=1;i<11;i++)calTable->setColumnWidth (i,53);

calTable->setRowHeight(0,36);
calTable->setRowHeight(1,36);
calTable->setRowHeight(2,36);
calTable->setFocusPolicy(Qt::NoFocus);

lb1 = new QTableWidgetItem( "AN" );
lb2 = new QTableWidgetItem( "ZV" );
lb3 = new QTableWidgetItem( "SV" );

calTable->setItem( 0, 0, lb1 );
lb1->setFont(QFont("Times", 12, QFont::Normal));
lb1->setBackground( labelbackground );
lb1->setTextAlignment(Qt::AlignCenter);

calTable->setItem( 1, 0, lb2 );
lb2->setFont(QFont("Times", 12, QFont::Normal));
lb2->setBackground( labelbackground );
lb2->setTextAlignment(Qt::AlignCenter);

calTable->setItem( 2, 0, lb3 );
lb3->setFont(QFont("Times", 12, QFont::Normal));
lb3->setBackground( labelbackground );
lb3->setTextAlignment(Qt::AlignCenter);

for( int i = 1; i < 11; i++ )
{
lb = new QTableWidgetItem( "AN" + QString().setNum(i));
lb->setTextAlignment( Qt::AlignCenter );
lb->setFont(QFont("Times", 12, QFont::Normal));
lb->setBackground( labelbackground );
analogLabel1 << (lb);

zv = new QTableWidgetItem();
zv->setTextAlignment(Qt::AlignCenter);
zv->setFont(QFont("Times", 12, QFont::Normal));
zv->setBackground( valuebackground );
zvdisplay << (zv);

sv = new QTableWidgetItem();
sv->setTextAlignment(Qt::AlignCenter);
sv->setFont(QFont("Times", 12, QFont::Normal));
sv->setBackground( valuebackground );
svdisplay << (sv);

calTable->setItem( 0, i, lb );
calTable->setItem( 1, i, zv );
calTable->setItem( 2, i, sv );
}
updateCalTable();
}

GimpMaster
11th September 2008, 18:50
The QTableWidget has the resizeColumnsToContents() method that resizes the columns. Also what I like to do is to resize the very last column to fit to the right edge of the table. You can do this with something like this...

QHeaderView *HorzHdr = ui.tableWidget->horizontalHeader();
HorzHdr->setStretchLastSection(true);

aamer4yu
11th September 2008, 19:16
For auto resizing of column widths, you can use the above method of resizeColumnsToContents.

But you need to call it everytime you columns change,
You may better use ui.tableWidget->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);

This will automatically resize columns. But you will not be able to allow user to resize columns or do it programitically. Read the docs.