Search code examples
pythonpython-3.xpyqt5qt-designerqtablewidget

Assign different widths to columns of a QTableWidget


I'm developing a small interface in pyqt5 from QT Designer, this includes a QTableWidget but I want to assign different widths to the columns, I have found topics that talk about the same but I don't know exactly where to insert the code they provide, I don't know if it's because of the version, I'm quite new in QT Designer.

I leave the questions I mention for what it's worth.

PyQt:How do i set different header sizes for individual headers?

PyQt Set column widths

The structure of my files is as follows:

app.py: Stores the functionalities of the application

SGS.py: The code that is generated after converting the .ui file to .py

SGS.ui

I leave the SGS.py part where the table headers are generated for what it's worth.

item = self.TableDocs.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "IDsystem"))
item = self.TableDocs.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "IDpeople"))
item = self.TableDocs.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "Work"))
item = self.TableDocs.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "Hours"))

I also leave the code with which I fill the table

result = Cur.execute("SELECT idsystem,IDpeople,work,hours FROM workers")
self.TableDocs.setRowCount(0)

for row_number, row_data in enumerate(result):
    self.TableDocs.insertRow(row_number)
    for column_number, data in enumerate(row_data):
        self.TableDocs.setItem(row_number, column_number, QtWidgets.QTableWidgetItem(str(data)))

Solution

  • The python file generated from the ui should never be edited. Consider it as a resource file (like an image or a json file) that is used to "create" the interface. Everything that you can't do from Designer you will have to implement in your application code files.

    You can set the size (or resize mode, for example automatic resizing, or the "stretching" to the available width) whenever a model is applied to the item view. Since you're using a QTableWidget (which has its internal private model), you can do that as soon as the widget is created in the interface, which is right after setupUi (or loadUi) if using a designer file.

    In order to set the size and behavior of the sections, you need to access the table headers: the horizontal one for the columns, the vertical for the rows.

    class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
        def __init__(self):
            super(MyWindow, self).__init__()
            self.setupUi(self)
    
            horizontalHeader = self.TableDocs.horizontalHeader()
            # resize the first column to 100 pixels
            horizontalHeader.resizeSection(0, 100)
            # adjust the second column to its contents
            horizontalHeader.setSectionResizeMode(
                1, QtWidgets.QHeaderView.ResizeToContents)
            # adapt the third column to fill all available space
            horizontalHeader.setSectionResizeMode(
                2, QtWidgets.QHeaderView.Stretch)
    

    Note that if you remove a column and insert another one in the same place, you'll need to set its section size or mode once again.