I have a QTableView inside a layout itself inside a Widget. How I can set the Widget size after the call of tableView.resizeColumnsToContents() in order to have the same size of the table (and does not need a scrollbar ) Thanks
Asked
Active
Viewed 1,971 times
1 Answers
2
If I understand properly what you want is to adjust the horizontal size of the container widget to the size of the table contents (which is not necessarily the table size). The following method works for me with a small table of 20 rows and 3 columns:
- ask the table model for the total number of columns (it should be returned by your implementation of the
columnCount
method) - ask the view for the width of every column using the
columnWidth
method and compute the total width of the columns - finally resize your container widget taking into account the total width of columns, the width of the table vertical header and the autoscroll margin
So the code would look like:
my_table_view.resizeColumnsToContents()
w = 0
for c in range(my_table_model.columnCount()):
w = w + my_table_view.columnWidth(c)
container_widget.resize(w + my_table_view.verticalHeader().width() + my_table_view.autoScrollMargin()*1.5, container_widget.size().height())
As I said, I'm assuming that you want to resize your widget horizontally.

Vicent
- 5,322
- 2
- 28
- 36
-
You understood perfectly my need! I will try this solution! – nam Nov 03 '12 at 21:29