我想在使用QWidget时更改标题的背景颜色。我尝试了以下方法,但它们不起作用:
QTreeWidgetItem * header = ui->treeWidget->headerItem();
header->setBackground(0, QBrush(QColor(185,192,201)));
header->setBackgroundColor(0, QColor(185,192,201));
setStyleSheet("QHeaderView::section { background-color:red }");
我也想知道如何隐藏标题的分界线?
你为什么不只使用样式表?
YourQTreeWidget QHeaderView::section {
background-color: red; // for the bakcground
border-right: none; // right-border of each section
border-left: none; // left border of each section
}
正如你可以阅读here(正如你在你的例子中看到的)setBackgroundColor
不适用于标题项(我怀疑这是由于标题和行项之间的差异)。你应该重新实现QHeaderView
或尝试上述选项。
我找到了一种方法来改变标题的样式,但我不知道为什么我以前的方法不起作用。
QHeaderView::section {
color: black;
padding: 2px;
height:20px;
border: 0px solid #567dbc;
border-left:0px;
border-right:0px;
background: #f9f9f9;
}