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