nam
nam

Reputation: 3632

How to resize the widget that hold a QTableView?

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

Upvotes: 0

Views: 2056

Answers (1)

Vicent
Vicent

Reputation: 5452

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.

Upvotes: 2

Related Questions