更改QTableVi中的默认行大小

2024-05-23 17:42:24 发布

您现在位置:Python中文网/ 问答频道 /正文

如何更改QTableView中的默认行大小以使其更小?我可以调用resizeRowsToContents(),但随后的插入仍会在末尾添加默认大小的新行。

我猜这可能与样式表有关,但我对影响视觉变化的因素还不够熟悉。

我在Python中使用PySide,但我认为这是一个一般的Qt问题。


示例视图:

默认表外观

enter image description here

表在resizeRowsToContents()之后的外观:

enter image description here

现在,如果我在末尾添加一个新的空行:

enter image description here

该死,它使用默认的行高和所有额外的空间。


Tags: 视图示例空间视觉qt样式表外观因素
1条回答
网友
1楼 · 发布于 2024-05-23 17:42:24

Qt::SizeHintRole负责单元格的宽度和高度。所以你可以:

someModel.setData(someIndex,QSize(width, height), Qt::SizeHintRole);

或者只是这个:

someView.verticalHeader().setDefaultSectionSize(10);

我认为setDefaultSectionSize正是你想要的。恐怕没有其他简单的方法了。

From doc:

defaultSectionSize : int

This property holds the default size of the header sections before resizing. This property only affects sections that have Interactive or Fixed as their resize mode.

如果您不知道需要多少像素,那么在使用rowHeight()应用resizeRowsToContents()之后,尝试获取此高度

所以应该是这样的:

resizeRowsToContents();
someView.verticalHeader().setDefaultSectionSize(someView.rowHeight(0));

相关问题 更多 >