Results 1 to 2 of 2

Thread: Resizing of QTableWidget and QMainWindow according to the table content

  1. #1
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Resizing of QTableWidget and QMainWindow according to the table content

    Hi all,

    I searched a lot but did not find a satisfying solution.

    Simple Situation

    using the designer to place QTableWidget (clist) inside a MainWindow. Adding a horizontal Layout to MainWindow.

    And using

    ui->clist->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);

    to resizing the coloums to fit the maximal content length. This works like expected

    Problem:

    The width of the table itself depends on the width of the MainWindow. If the MainWindow is to small the Table shows only some of the columns and one has to use the scroll bar. If the MainWindow is to wide the tablewidth is also to wide.

    So how to force the MainWindow to resize to the width of the table showing all columns ?

    I tried to get the width of the table, but it depends on the width of the MainWindow and not on the sum of the column width.
    so i tried:

    Qt Code:
    1. QTableWidget *t = ui->clist;
    2. t->setVisible(false);
    3. t->resizeColumnsToContents();
    4. int sum = 0;
    5. for(int i = 0; i < 10; i++ )
    6. {
    7. sum += t->columnWidth(i);
    8. }
    9. QRect g = t->geometry();
    10. g.setWidth(sum);
    11. t->setGeometry(g);
    12. t->setVisible(true);
    13. g= this->geometry();
    14. g.setWidth(sum+100);
    15. this->setGeometry(g);
    To copy to clipboard, switch view to plain text mode 

    This works more or less. But sum is only the “inner size” of the table so one has to add some additional space. (which did not fit really)

    I’m sure that there is a much more simple solution to adjust the width of the MainWindow so that all columns of the table are visible but not more. Also if the content width of the table changes (because of adding/changing content) the MainWindow should also change.

    So dear Gurus what did I miss ?? What is the solution ??

    Thanks and have a good time
    dexli

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Resizing of QTableWidget and QMainWindow according to the table content

    So how to force the MainWindow to resize to the width of the table showing all columns ?
    You don't, at least not in programs that behave in the way most people expect.

    If you really must do it then you probably need to start with QHeaderView::length() and force the minimum width of the QTableView. Don't forget to handle the case of the able content being wider than the screen.

Similar Threads

  1. Replies: 1
    Last Post: 10th January 2012, 15:41
  2. Resizing QML and QMainWindow
    By hungarycoder in forum Qt Quick
    Replies: 3
    Last Post: 19th February 2011, 19:12
  3. link the content og a QComboBox to a databse table
    By graciano in forum Qt Programming
    Replies: 2
    Last Post: 16th June 2010, 22:08
  4. Resizing a QDialog to the content size
    By Nyphel in forum Qt Programming
    Replies: 8
    Last Post: 15th March 2007, 09:16
  5. Prevent from resizing a QMainWindow
    By Flier in forum Qt Tools
    Replies: 5
    Last Post: 14th April 2006, 18:11

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.