PyQt5 QTableWidget(表单控件)自适应窗口大小、栏位大小调整及布局 您所在的位置:网站首页 如何自动调整表格大小设置 PyQt5 QTableWidget(表单控件)自适应窗口大小、栏位大小调整及布局

PyQt5 QTableWidget(表单控件)自适应窗口大小、栏位大小调整及布局

2024-05-27 03:13| 来源: 网络整理| 查看: 265

目录

前言

用QtDesigner设计UI

enum QHeaderView::ResizeMode详细说明及使用

cascadingSectionResizes : bool详细说明

CascadingSectionResizes图解说明

resizeSection(int logicalIndex, int size)详细说明

 

前言

需要将QTableWidget(表单控件)的大小重新调整,去PyQt5的官网(https://pyqt.readthedocs.io/en/latest/sip-classes.html)查询资料,结果是这样的~~

还好,我有C++ Qt Help(帮助文档)O(∩_∩)O哈哈~

本文旨在介绍QTableWidget(表单控件)的自适应窗口大小、栏位大小调整及布局。

用QtDesigner设计UI

1. 先用QtDesigner设计一个简单的UI,使用QTableWidget(表单控件),在其中添加如下信息,

主窗口尺寸设定为1000*400(Width*Height), QTableWidget的最大尺寸之高度Height设定为290(这个后续会用到),对MainWindow做垂直布局(Lay Out vertically)。设计完按Ctrl+R预览,UI如下图,因为高度不够,所以第8行不能显示。

笔者说明:Index栏是故意多加的,后面会用到。

enum QHeaderView::ResizeMode详细说明及使用

重点来了. 

以下表格来自C++ Qt5.9.3 QtCreator 4.4.1 Help(帮助文档),里面有加入笔者的一些说明

以水平方向表头horizontalHeader举例:

Constant

Value

Description

QHeaderView::Interactive

0

The user can resize the section. The section can also be resized programmatically using resizeSection(). The section size defaults to defaultSectionSize. (See also cascadingSectionResizes.)默认是每栏等宽显示,但是用户可以根据需要自己调整每列的宽度。需要注意的是,如果设置为Interactive mode,那么水平方向的表头是不能隐藏的。如果隐藏表头,用户无法调整宽度。大家自己试验一下。用户可以调整每栏宽度。可以使用resizeSection()函数在代码中来为每栏调整尺寸。每栏的默认大小为defaultSectionSize。(可以参考cascadingSectionResizes.)

QHeaderView::Fixed(QHeaderView::Custom)

2

The user cannot resize the section. The section can only be resized programmatically using resizeSection(). The section size defaults to defaultSectionSize.用户不能调整每栏宽度。只可以使用resizeSection()函数在代码中来为每栏调整尺寸。每栏的默认大小为defaultSectionSize。默认情况下每栏宽度固定,用户也不可以调整每列的宽度。

QHeaderView::Stretch

1

QHeaderView will automatically resize the section to fill the available space. The size cannot be changed by the user or programmatically.自动调整每栏大小来填充可用区域。用户与代码都不可调整其大小。设置表格为自适应的伸缩模式,即可根据窗口的大小来改变网格的大小,Stretch mode为等宽或等高# 如果设置了Resize mode为Stretch, 最后一列拉伸处理[setStretchLastSection(True)]就会失灵

QHeaderView::ResizeToContents

3

QHeaderView will automatically resize the section to its optimal size based on the contents of the entire column or row. The size cannot be changed by the user or programmatically. (This value was introduced in 4.2)根据每栏/每行内容来调整最优大小。用户与代码都不可调整其大小。(这是Qt4.2加入的)默认情况下每栏宽度根据内容显示,但是用户不可以调整每列的宽度。

 

垂直方向表头verticalHeader与horizontalHeader原理相同,大家可以尝试不同的参数来找到合适自己的风格。

笔者提示:

1. 需要包含对应的模块

from PyQt5.QtWidgets import QHeaderView

2. 在用PyQt5时将两个冒号::替换成英文的句号.即可使用,例如QHeaderView.ResizeToContents

3. PyQt5的函数调用如下:

self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive)

4. 将各种模式组合在一起,例如“QHeaderView.Interactive | QHeaderView.Stretch”是没用的,大家自己试验下

self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive | QHeaderView.Stretch)

 

有两个函数要跟大家分享的是:

cascadingSectionResizes : bool详细说明

This property holds whether interactive resizing will be cascaded to the following sections once the section being resized by the user has reached its minimum size

This property only affects sections that have Interactive as their resize mode.

The default value is false.

This property was introduced in Qt 4.2.

以上内容摘自C++ QtCreator 4.4.1 Help(帮助文档)

笔者翻译:

一旦某个栏位被用户调整到最小值尺寸,这个属性决定了用户调整大小的方式是否要影响到其周遭的栏位。(笔者补充:即所谓的层叠反应或级联反应。)

此属性仅影响将resize mode设定为Interactive模式的栏位。

默认值为false。

该属性在Qt 4.2中引入。

笔者提示:

这个你可以在代码中这样实现

self.tableWidget.horizontalHeader().setCascadingSectionResizes(True)

也可以通过QtDesigner来设定(懒人做法做法)

 

CascadingSectionResizes图解说明

以水平方向为例:

horizontalHeaderCascadingSectionResizes

如果将其设定为False(默认值),那么其中一栏调整到最小时,其他栏位是动不了的,以调整Status栏位为例,将其向左调整最小,如下图所示:

另外,可以看到,在我的系统上,horizontalHeaderMinumumSectionSize(水平方向最小尺寸)为31

如果将其设定为True,那么其中一栏调整到最小时,其他栏位可以被挤压,如下图所示

resizeSection(int logicalIndex, int size)详细说明

void QHeaderView::resizeSection(int logicalIndex, int size)

Resizes the section specified by logicalIndex to size measured in pixels. The size parameter must be a value larger or equal to zero. A size equal to zero is however not recommended. In that situation hideSection should be used instead.

笔者翻译:调整这个参数“logicalIndex”指定的栏位大小为第二个参数“size”,单位是pixel(像素/像元)。第二个参数“size”必须大于或等于零。不过,不建议“size”参数等于零。建议使用函数hideSection来替代“size”参数等于零的情况。

笔者提示:

logicalIndex说明是从0开始的。要使用这个函数,QHeaderView::ResizeMode只能是QHeaderView::Interactive或QHeaderView::Fixed

在PyQt5中我们就可以这样使用:

self.tableWidget.horizontalHeader().resizeSection(0, 100)  # 调整第一列的大小为100像素

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有