1

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)))
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Jalkhov
  • 167
  • 5
  • 18

1 Answers1

3

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.

musicamante
  • 41,230
  • 6
  • 33
  • 58
  • Perfect, this has worked for me. Thank you. I don't know if I can make another query right here but I wouldn't want to open another question, can I insert buttons in each cell next to its respective record? I mean, the data in that table is loaded from a SQLite database, so, I would like to generate an extra column that has buttons, like delete, modify and so on. As for the functions of the buttons I think I could do it by myself, I just want to know how to generate those buttons for each row. – Jalkhov Dec 17 '19 at 02:01
  • 1
    You can use [`setCellWidget()`](https://doc.qt.io/qt-5/qtablewidget.html#setCellWidget), but if you need further help, it's better to create a new question. – musicamante Dec 17 '19 at 07:09