Results 1 to 2 of 2

Thread: Relative re-sizing of QTableWidget's columns

  1. #1
    Join Date
    Sep 2012
    Location
    Pune, India
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Relative re-sizing of QTableWidget's columns

    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.

  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: Relative re-sizing of QTableWidget's columns

    A rough solution
    Qt Code:
    1. class TableWidget : public QTableWidget
    2. {
    3. public:
    4. explicit TableWidget(QWidget * parent = 0)
    5. : QTableWidget(parent)
    6. {
    7. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    8. horizontalHeader()->setStretchLastSection(true);
    9. }
    10.  
    11. TableWidget(int rows, int columns, QWidget *parent = 0)
    12. : QTableWidget(rows, columns, parent)
    13. {
    14. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    15. horizontalHeader()->setStretchLastSection(true);
    16. }
    17.  
    18. protected:
    19. void resizeEvent(QResizeEvent * event)
    20. {
    21. if(model() and model()->columnCount())
    22. for(int column = 0; column < model()->columnCount(); column++)
    23. setColumnWidth(column, event->size().width() / model()->columnCount());
    24. }
    25. };
    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.

Similar Threads

  1. [QTableWidget] Columns sizes
    By Macok in forum Qt Programming
    Replies: 1
    Last Post: 9th March 2009, 08:26
  2. Resizing QTableWidget Columns
    By mbrusati in forum Qt Programming
    Replies: 4
    Last Post: 29th September 2008, 23:26
  3. QTableWidget - images in columns
    By bruccutler in forum Qt Programming
    Replies: 3
    Last Post: 1st May 2007, 19:28
  4. Automatic sizing of QTableWidget's cells
    By Artem Anisimov in forum Qt Programming
    Replies: 2
    Last Post: 4th September 2006, 20:01
  5. Maximize columns on QTableWidget
    By marcelrc in forum Newbie
    Replies: 1
    Last Post: 21st May 2006, 16:34

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.