PySide2 - 更改表头背景颜色

问题描述 投票:0回答:1

我现在已经是默认QTableWidget的。我只是改变了背景色行甚至索引。我想在表头做同样的。

enter image description here

我试了一下 - self.horizontalHeaderItem(...).setBackgroundColor(QColor(...))self.horizontalHeaderItem(...).setBackground(QBrush(...)) - 失败。

我试过未来 - self.horizontalHeader().setStyleSheet("::section {" "background-color: lightblue; }") - 我设法改变背​​景颜色。这些问题是:(1)我不太明白这是什么代码实际上是因为我不能PySide2文档中找到它,(2)中的代码改变不仅背景,但许多头的外观特征。

我怎样才能改变只有头背景?或者更一般 - 如何调整表头外观PySide2?

enter image description here

python python-3.x pyside2
1个回答
1
投票

我在这里发布我在我寻求答案了解到 - 2个可能的解决方案。

我想从notImplementednotDocumented,与PySide2 notExplained问题有些纠结混合问题起源。我希望情况会在未来变得更好。现在的答案。

这个问题可以得到解决这样的(并且可能不会):

plt = QPalette()
plt.setColor(QPalette.Button, QColor(128, 0, 0))
my_table_widget.horizontalHeader().setPalette(plt)

QPalette控制窗口小部件的颜色。但外表控制的另一层 - 风格。我用PySide2上Win10x64所以默认的样式是windowsvista与总可用3种风格:fusionwindowswindowsvistaWindowsvista风格忽略最调色板颜色。因此,这种方法的工作应该改变整个应用程序的样式(或只有一些小部件?):

app.setStyle(QStyleFactory.create('fusion'))

我想出了另一种解决方案是(最好对我来说):

my_table_widget.horizontalHeader().setStyleSheet('''
    ::section {
        background-color: lightgray;
        border-style: flat;
        padding: 0px 5px;
        }''')

和...一起:

my_table_widget.horizontalHeader().setDefaultAlignment(Qt.AlignLeft | Qt.AlignVCenter)

我想这是一个样式表来调整按钮的外观。我不太明白section词的含义。此外,使用这种方法,应该使用谷歌和QT5文档。如何我的表看起来与应用的第二个解决方案:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.