1. 程式人生 > >PyQt5 QTableWidget(表單控制元件)自適應視窗大小、欄位大小調整及佈局(一)

PyQt5 QTableWidget(表單控制元件)自適應視窗大小、欄位大小調整及佈局(一)

目錄

前言

前言

還好,我有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 modeStretch 最後一列拉伸處理[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來設定(個人推薦此種做法)

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”引數等於零的情況。

筆者提示:

  1. logicalIndex說明是從0開始的。
  2. 要使用這個函式,QHeaderView::ResizeMode只能是QHeaderView::Interactive或QHeaderView::Fixed

在PyQt5中我們就可以這樣使用:

self.tableWidget.horizontalHeader().resizeSection(0, 100)  # 調整第一列的大小為100畫素