Important: Please read the Qt Code of Conduct -

QTablewidget and Mainwindow resizing according to content of Table

  • 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.


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


    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:

    QTableWidget *t = ui->clist;
    int sum = 0;
    for(int i = 0; i < 10; i++ )
    sum += t->columnWidth(i);
    QRect g = t->geometry();
    g= this->geometry();

    This works more or less. But it is only the "inner size" of the table.

    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 the MainWindow should be change also.

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

    Thanks and have a good time

Log in to reply