总结
在使用QTableView进行列表内容展示的时候,经常设置列首的背景颜色和交替行的颜色(单、双行不同颜色)。下面记录一下如何通过在QSS设置样式表来实现。
首先,不能使用如下的方法进行列首背景颜色的设置:
QTableWidget#tableWidget_HistItems::horizontalHeader QHeaderView::section {background-color: #253b91;color: white;}
或
QTableWidget#tableWidget_HistItems QHeaderView::section {background-color: #253b91;color: white;}
因为QHeaderView并不是QTableWidget的子控件,只有子控件才可以使用::方法访问并设置。因此,要采用如下方式设置:
1、使用全局选择器,QHeaderView::section,设置样式如下:
QHeaderView::section{background-color:#253b91;color: white;font-family: "Microsoft YaHei Light";font-size: 16px;}
2、不仅要设置QHeaderView::section,还要再设置QHeaderView:。如果只单独设置QHeaderView::section,鼠标点击某一行时,列首的样式会发生变化。样式设置如下:
QHeaderView{background-color:#253b91;color: white;font-family: "Microsoft YaHei Light";font-size: 16px;}QHeaderView::section{background-color:#253b91;color: white;font-family: "Microsoft YaHei Light";font-size: 16px;}
3、关于交替行的颜色设置,如下:
QTableWidget#tableWidget_HistItems {background-color: #062342;alternate-background-color:#0f519c;border: 1px solid transparent;color:white;font:16px;border-radius: 3px;qproperty-alternatingRowColors: "true";}
qproperty-alternatingRowColors: "true" 用于启用交替行颜色
alternate-background-color 设置交替行的颜色