问题描述
我正在 qt designer 在 pyqt5 中开发一个小界面,其中包括一个 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.