Results 1 to 3 of 3

Thread: auto resizing column/row widths

  1. #1
    Join Date
    Feb 2008
    Posts
    46
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default auto resizing column/row widths

    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();
    }
    thanks,
    raman

  2. #2
    Join Date
    Aug 2008
    Location
    Texas, USA
    Posts
    44
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: auto resizing column/row widths

    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);

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: auto resizing column/row widths

    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.

Similar Threads

  1. hide() and auto resizing
    By MrGarbage in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2007, 09:44
  2. form not auto resizing
    By quickNitin in forum Newbie
    Replies: 5
    Last Post: 7th June 2007, 10:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.