PyQT Table Set Adaptive Width to Fit Resized Window: A Beginner Guide – PyQT Tutorial

By | December 13, 2019

When you have created a table using pyqt, you may find this table will have a default width. However, this width can not changed when resizing window. To fix this problem, we will tell you how to do in this tutorial.

For example, the table with default width likes:

pyqt table with default width

When resizing widow, you may find the width of table is not changed.

pyqt table width is not changed when resizing window

What is table adaptive width?

It means the width of table is changed when resizing window.

How to set pyqt table adaptive width?

It is very simple in pyqt5. Here is an example.

First, import some libraries.

from PyQt5.QtWidgets import QTableWidget,QTableWidgetItem, QHeaderView

Set adaptive width for table

hheader = table.horizontalHeader()
hheader.setSectionResizeMode(QHeaderView.Stretch)

Where table is QTableWidget object. Run this code, you will find the effect:

pyqt table set adaptive width

One thought on “PyQT Table Set Adaptive Width to Fit Resized Window: A Beginner Guide – PyQT Tutorial

Leave a Reply