几种设置模式
先介绍一下:
setDefaultSectionSize
# 设置固定列宽为80
table_obj.horizontalHeader().setDefaultSectionSize(80)
内置调整模式
# 用户可调整,默认值为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};"
)