是否可以更改 QTableWidget 行标签的颜色? [英] Is it possible to change the colour of a QTableWidget row label?

查看:65
本文介绍了是否可以更改 QTableWidget 行标签的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个继承自 QTableWidget 的类,我想知道是否可以更改表格中每一行的行标签颜色?

I have a class that inherits from QTableWidget and I'm wondering if it's possible to change the colour of the row label for each row in the table?

我不想更改任何单元格或列标题的颜色.

I don't want to change the colour of any of the cells or column headings.

谢谢:)

附言我希望每一行标签都有不同的颜色.动机是我可以使用这些颜色作为键/图例,因为表格中的每一行都对应于绘图上不同颜色的线.

P.S. I would like each row label to have a different colour. The motivation is that I can use these colours as a key/legend as each row in the table corresponds to a differently coloured line on a plot.

说明我所指表格元素的图像:

Image illustrating the elements of the table I am referring to:

推荐答案

是的,但只需要一个小技巧.使用 QTableWidgetsetVerticalHeaderItem,您甚至可以为标题行设置 QTableWidgetItem,并且您可以为每一行定义背景画笔.然而大多数时候背景会被忽略,因为实际的 QStyle 会覆盖它.将垂直标题小部件的样式设置为不会更改背景的样式,但可以解决问题.

Yes it is possible but only with a slight trick. With setVerticalHeaderItem of QTableWidget you can set a QTableWidgetItem even for header rows and there you can define a background brush for each row. However most of the times the background will be ignored because the actual QStyle will override it. Setting the style of the vertical header widget to a style that doesn't change the background however does the trick.

示例:

from PySide import QtGui

app = QtGui.QApplication([])

table = QtGui.QTableWidget(4, 2)
table.show()

for i in range(0, 4):
    item = QtGui.QTableWidgetItem('Text')
    item.setBackground(QtGui.QColor(i*50, i*30, 200-i*40))
    table.setVerticalHeaderItem(i, item)

# print(QtGui.QStyleFactory.keys())
table.verticalHeader().setStyle(QtGui.QStyleFactory.create('CleanLooks'))

app.exec_()

这篇关于是否可以更改 QTableWidget 行标签的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆