Results 1 to 7 of 7

Thread: How to set the QTableWidget's columns width to fit the widget

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default How to set the QTableWidget's columns width to fit the widget

    I'm using a QTableWidget, but I'm looking for a way to dynamically resize the columns to fit the widget base width.

    I tried this, but it's resizing the column contents only to fit it's column. But I need the columns resize itself to the widget's width.
    Qt Code:
    1. for(int nCol, nRow = 0; nRow < 3; nRow++)
    2. {
    3. for(nCol = 0; nCol < 4; nCol++)
    4. {
    5. vulnTable->setItem(nRow, nCol, new QTableWidgetItem(QString::number(nRow) + " " + QString::number(nCol)));
    6. vulnTable->resizeRowsToContents();
    7. vulnTable->resizeColumnsToContents();
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    And the result is as shown in the pic. How do I avoid the gap left-out?
    QTableWidget.png

    Thank you.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to set the QTableWidget's columns width to fit the widget

    Use this
    Qt Code:
    1. QTableWidget::horizontalHeader().setStretchLastSection(true);
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    rawfool (5th June 2013)

  4. #3
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to set the QTableWidget's columns width to fit the widget

    use this
    ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);

  5. #4
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to set the QTableWidget's columns width to fit the widget

    For QTableWidget::horizontalHeader().setStretchLastSec tion(true);
    Qt Code:
    1. error: C2228: left of '.setStretchLastSection' must have class/struct/union
    2. type is 'QHeaderView *'
    3. did you intend to use '->' instead?
    To copy to clipboard, switch view to plain text mode 

    And if I try this - tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    Qt Code:
    1. error: C2027: use of undefined type 'QHeaderView'
    2. error: C2065: 'Stretch' : undeclared identifier
    To copy to clipboard, switch view to plain text mode 

    NB: It's not even giving suggestions after horizontalHeader(). I'm using Qt 5.0.2
    Last edited by rawfool; 4th June 2013 at 07:46.

  6. #5
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to set the QTableWidget's columns width to fit the widget

    Qt Code:
    1. QTableWidget::horizontalHeader().setStretchLastSection(true);
    To copy to clipboard, switch view to plain text mode 
    Worked. Thanks

  7. #6
    Join Date
    Aug 2014
    Location
    bangalore
    Posts
    4
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Android

    Default Re: How to set the QTableWidget's columns width to fit the widget

    Quote Originally Posted by pradeepreddyg95 View Post
    use this
    ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    Hi... you suggestion was useful

  8. #7
    Join Date
    Sep 2015
    Location
    Cairo
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to set the QTableWidget's columns width to fit the widget

    I have another problem after doing this, header then cut like the this image Capture.PNG

    the second column in the first table is [Manufacturer]
    the second column header is [Apical Diameter] and the third column is [Occlusal Diameter]

    how can i make the table fit width of both header and content and also the columns resize itself to the widget's width.

    thanks

Similar Threads

  1. Replies: 0
    Last Post: 12th November 2012, 14:12
  2. Width QTableView for QCombobox with multiple columns
    By visor_ua in forum Qt Programming
    Replies: 7
    Last Post: 21st June 2011, 11:54
  3. Replies: 1
    Last Post: 13th August 2010, 07:28
  4. set the width of the columns of QTableView
    By zhanglr in forum Qt Programming
    Replies: 6
    Last Post: 31st July 2008, 16:29
  5. Replies: 1
    Last Post: 19th September 2007, 11:32

Tags for this Thread

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.