PyQt5 QTableWidget列宽设置方法
十、PyQt5 QTableWidget列宽设置方法
#pyqt5 表格怎么点击的时候选择一整行呢
from PyQt5.QtWidgets import QAbstractItemView
self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
几种设置模式
先介绍一下:
setDefaultSectionSize
# 设置固定列宽为80
table_obj.horizontalHeader().setDefaultSectionSize(80)
内置调整模式
#设置每个列宽 模式设置一
self.table.setColumnWidth(0,50)
self.table.setColumnWidth(1,100)
self.table.setColumnWidth(2, 500)
self.table.setColumnWidth(3, 50)
self.table.setColumnWidth(4, 50)
#设置整个表格宽度
self.table.setFixedWidth(783)
#模式设置二
# 用户可调整,默认值为setDefaultSectionSized的值
table_obj.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive)
# 用户不可调整,默认值为setDefaultSectionSized的值
table_obj.horizontalHeader().setSectionResizeMode(QHeaderView.Fixed)
# 用户不可调整,自动平分适应可用区域
table_obj.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
# 用户不可调整,自动适应内容的宽度
table_obj.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
# 用户可调整,默认值为setDefaultSectionSized的值
table_obj.horizontalHeader().setSectionResizeMode(QHeaderView.Custom)
使用方式:
各种调整模式配合、若有需要再配合setDefaultSectionSize
使用,基本能达到要求.
说个例子(区域自适应,指定列宽度)抛砖引玉,都可根据需求自由组合使用:
# 1 设置表格所有列在区域内自适应
table_obj.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
# 2 若此时觉得第一列太小(第一列的效果覆盖第一步的效果)
table_obj.horizontalHeader().setDefaultSectionSize(80)
# 3 设置第一列固定宽度
table_obj.horizontalHeader().setSectionResizeMode(0, QHeaderView.Fixed)
QSS设置(min-height:25px;min-width:40px)
table_obj.horizontalHeader().setStyleSheet(
"QHeaderView::section{background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
"stop:0 #fea356, stop: 0.5 #eeeeee,stop: 0.6 #eeeeee, stop:1 #fea356);"
"border:1px solid rgb(201,202,202);border-left:none;"
"min-height:25px;min-width:40px;font-weight:bold;font-size:13px};"
)
评论列表
发表评论