为 QTableWidget 的列分配不同的宽度

Assign different widths to columns of a QTableWidget(为 QTableWidget 的列分配不同的宽度)

本文介绍了为 QTableWidget 的列分配不同的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 QT Designerpyqt5 中开发一个小界面,其中包括一个 QTableWidget 但我想分配不同的列的宽度,我找到了谈论相同的主题,但我不知道在哪里插入他们提供的代码,我不知道是不是因为版本,我很新在 QT 设计器中.

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.

我会留下我提到的问题.

PyQt:如何设置不同的标题大小对于单个标题?

PyQt 设置列宽

我的文件结构如下:

app.py:存储应用程序的功能

SGS.py: .ui文件转换成.py

SGS.ui

我将生成表头的 SGS.py 部分保留,因为它的价值.

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"))

我也留下了填写表格的代码

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)))

推荐答案

从 ui 生成的 python 文件永远不要被编辑.将其视为用于创建"接口的资源文件(如图像或 json 文件).您无法通过 Designer 完成的所有操作都必须在您的应用程序代码文件中实现.

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.

每当模型应用于项目视图时,您都可以设置大小(或调整大小模式,例如自动调整大小,或拉伸"到可用宽度).由于您使用的是 QTableWidget(它具有其内部私有模型),因此您可以在界面中创建小部件后立即执行此操作,即在 setupUi (或 loadUi) 如果使用设计器文件.

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.

这篇关于为 QTableWidget 的列分配不同的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为 QTableWidget 的列分配不同的宽度