PyQt 设置列宽 [英] PyQt Set column widths

查看:74
本文介绍了PyQt 设置列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 QTableView,我基本上想模仿这个:

I've got a simple QTableView and I essentially want to emulate this:

view.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)

但是,我实际上不能使用它,因为这会在每一行上调用 sizeHint,这在我的情况下是不可接受的,因为小部件需要在启动时响应并且计算需要很长时间对于所有行.因此,这不是一个选项.

However, I cannot actually use this, since this calls sizeHint on every single row, which is unacceptable in my case, since the widget needs to be responsive on startup and the calculations take a long time for all the rows. Therefore this is not an option.

我知道单列的大小,它总是相同的,并且总是高度最大的那一列.但是,此单元格的宽度可能并不总是一行中所有单元格中最宽的.我通过这样做使这些单元格的大小正确,但这显然使所有单元格的大小相同,而且现在许多单元格太宽了.

I know what the size of a single column, which is always the same, and is always the one with largest height. However, the width of this cell may not always be the widest of all the cells in a row. I've made those cells the correct size by doing this, but this obviously makes all the cells the same size, and many are now far too wide.

view.horizontalHeader().setDefaultSectionSize(200)
view.verticalHeader().setDefaultSectionSize(100)

我也尝试过设置单独的列宽,但这似乎对宽度没有任何影响,如下所示:

I've also tried to set individual column widths, but that seems to have no effect at all on the widths, like this:

view.setColumnWidth(0, 5)

此外,设置单个列的宽度也不是很好,因为我无法提前知道它们的宽度.

Also, setting individual column widths isn't that great, since I can't know in advance how wide they're going to be.

有什么方法可以使用(或模拟)ResizeToContents 方法(如上所示),但不需要检查所有单元格大小?

Is there any way to use (or emulate) the ResizeToContents approach (as shown above), but in a way that wouldn't require checking all the cell sizes?

推荐答案

您是否尝试过使用 setResizeContentsPrecision ?

Have you tried to optimize the ResizeToContents mode with setResizeContentsPrecision ?

设置使用 ResizeToContents 时 QHeaderView 计算大小的精确度.较低的值会提供不太准确但快速的自动调整大小,而较高的值会提供更准确的调整大小,但可能会很慢.

Sets how precise QHeaderView should calculate the size when ResizeToContents is used. A low value will provide a less accurate but fast auto resize while a higher value will provide a more accurate resize that however can be slow.

数字精度指定计算首选大小时应考虑的节数.

The number precision specifies how many sections that should be consider when calculating the preferred size.

默认值为 1000,这意味着在执行自动调整大小时,具有自动调整大小的水平列将在计算时最多查看 1000 行.

The default value is 1000 meaning that a horizontal column with auto-resize will look at maximum 1000 rows on calculating when doing an auto resize.

特殊值 0 表示它只会查看可见区域.特殊值 -1 表示查看所有元素.

这篇关于PyQt 设置列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆