Python PySide - PyQt:如何将 QTableWidget 列宽设置为可用空间的比例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38098763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PySide - PyQt : How to make set QTableWidget column width as proportion of the available space?
提问by AdrienW
I'm developing a computer application with PySideand I'm using the QTableWidget. Let's say my table has 3 columns, but the data they contain is very different, like (for each row) a long sentence in the first column, then 3-digit numbers in the two last columns. I'd like to have my table resize in order to adjust its size to the data, or at least to be able to set the column sizes as (say) 70/15/15 % of the available space.
我正在使用PySide开发计算机应用程序,并且正在使用 QTableWidget。假设我的表有 3 列,但它们包含的数据非常不同,例如(对于每一行)第一列中有一个长句子,然后是最后两列中的 3 位数字。我想让我的表调整大小,以便根据数据调整其大小,或者至少能够将列大小设置为(例如)可用空间的 70/15/15%。
What is the best way to do this ?
做这个的最好方式是什么 ?
I've tried table.horizontalHeader().setResizeMode(QHeaderView.Stretch)
after reading this questionbut it makes 3 columns of the same size.
我table.horizontalHeader().setResizeMode(QHeaderView.Stretch)
在阅读这个问题后尝试过,但它制作了 3 个相同大小的列。
I've also tried table.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
thanks to Fabio's commentbut it doesn't fill all the available space as needed.
table.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
感谢Fabio的评论,我也尝试过,但它没有根据需要填充所有可用空间。
Neither Interactive
, Fixed
, Stretch
, ResizeToContents
from the QHeaderView documentationseem to give me what I need (see second edit).
无论是Interactive
,Fixed
,Stretch
,ResizeToContents
从QHeaderView文档似乎给我我需要什么(见第二编辑)。
Any help would be appreciated, even if it is for Qt/C++! Thank you very much.
任何帮助将不胜感激,即使它是针对Qt/C++ 的!非常感谢。
EDIT :I found kind of a workaround but it's still not what I'm looking for :
编辑:我找到了一种解决方法,但它仍然不是我想要的:
header = table.horizontalHeader()
header.setResizeMode(QHeaderView.ResizeToContents)
header.setStretchLastSection(True)
It would be better if there existed a setStretchFirstSection
method, but unfortunately there does not seem to be one.
要是有setStretchFirstSection
方法就好了,可惜好像没有。
EDIT 2 :
编辑 2:
The only thing that can be modified in the table is the last column, the user can enter a number in it. Red arrows indicates what I'd like to have.
表格中唯一可以修改的是最后一列,用户可以在其中输入数字。红色箭头表示我想要的。
Here's what happens with Stretch
回答by ekhumoro
This can be solved by setting the resize-mode for each column. The first section must stretch to take up the available space, whilst the last two sections just resize to their contents:
这可以通过为每列设置调整大小模式来解决。第一部分必须伸展以占用可用空间,而最后两部分只是调整其内容的大小:
PyQt4:
PyQt4:
header = self.table.horizontalHeader()
header.setResizeMode(0, QtGui.QHeaderView.Stretch)
header.setResizeMode(1, QtGui.QHeaderView.ResizeToContents)
header.setResizeMode(2, QtGui.QHeaderView.ResizeToContents)
PyQt5:
PyQt5:
header = self.table.horizontalHeader()
header.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
回答by Xavi Martínez
PyQt4
PyQt4
header = self.table.horizontalHeader()
header.setResizeMode(0, QtGui.QHeaderView.Stretch)
header.setResizeMode(1, QtGui.QHeaderView.ResizeToContents)
header.setResizeMode(2, QtGui.QHeaderView.ResizeToContents)
header.setResizeMode(3, QtGui.QHeaderView.Stretch)
PyQt5
PyQt5
header = self.table.horizontalHeader()
header.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
header.setSectionResizeMode(3, QtWidgets.QHeaderView.Stretch)
回答by koxx
As mentioned before, you can do this by setting the resize-mode of each column. However, if you have a lot of columns this can be a lot of code. The way I do it is setting the "general" resize-mode to "ResizeToContent" and than for one (or more) columns to "Stretch"!
如前所述,您可以通过设置每列的调整大小模式来做到这一点。但是,如果您有很多列,这可能是很多代码。我这样做的方法是将“常规”调整大小模式设置为“ResizeToContent”,而不是将一个(或多个)列设置为“拉伸”!
Here is the code:
这是代码:
PyQt4:
PyQt4:
header = self.table.horizontalHeader()
header.setResizeMode(QtGui.QHeaderView.ResizeToContents)
header.setResizeMode(0, QtGui.QHeaderView.Stretch)
PyQt5:
PyQt5:
header = self.table.horizontalHeader()
header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
header.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
回答by Brendan Abel
You can do this with QItemDelegates
or QStyledItemDelegates
. If you want to resize to contents andhave automatic stretch, you'll need to choose which column is the "stretch" column.
您可以使用QItemDelegates
或来执行此操作QStyledItemDelegates
。如果要根据内容调整大小并自动拉伸,则需要选择哪一列是“拉伸”列。
class ResizeDelegate(QStyledItemDelegate):
def __init__(self, table, stretch_column, *args, **kwargs):
super(ResizeDelegate, self).__init__(*args, **kwargs)
self.table = table
self.stretch_column = stretch_column
def sizeHint(self, option, index):
size = super(ResizeDelegate, self).sizeHint(option, index)
if index.column() == self.stretch_column:
total_width = self.table.viewport().size().width()
calc_width = size.width()
for i in range(self.table.columnCount()):
if i != index.column():
option_ = QtGui.QStyleOptionViewItem()
index_ = self.table.model().index(index.row(), i)
self.initStyleOption(option_, index_)
size_ = self.sizeHint(option_, index_)
calc_width += size_.width()
if calc_width < total_width:
size.setWidth(size.width() + total_width - calc_width)
return size
...
table = QTableWidget()
delegate = ResizeDelegate(table, 0)
table.setItemDelegate(delegate)
... # Add items to table
table.resizeColumnsToContents()
You can set the resize mode to ResizeToContents
, or if you want the user to be able to adjust the column width as needed, just call resizeColumnsToContents
manually after making changes to the table items.
您可以将调整大小模式设置为ResizeToContents
,或者如果您希望用户能够根据需要调整列宽,只需resizeColumnsToContents
在更改表格项目后手动调用即可。
You also may need to fudge around with the width calculations a bit because of margins and padding between columns (like add a pixel or two to the calculated_width
for each column to account for the cell border).
由于列之间的边距和填充(例如calculated_width
为每列添加一个或两个像素以考虑单元格边框),您可能还需要稍微调整宽度计算。