我需要在QTreeView上更改所选项目的背景颜色。 我已尝试使用this示例和this Stack Overflow问题。
应用于:: branch子控件的样式工作正常,但:: item子控件中的所有命令都不起作用。
QSS应用于QTreeView:
QTreeView {
show-decoration-selected: 1;
}
QTreeView::item {
border: 1px solid #d9d9d9;
border-top-color: transparent;
border-bottom-color: transparent;
background: rgb(255, 0,0);
}
QTreeView::item:hover {
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1);
border: 1px solid #bfcde4;
}
QTreeView::item:selected {
border: 1px solid #567dbc;
}
QTreeView::item:selected:active{
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6ea1f1, stop: 1 #567dbc);
}
QTreeView::item:selected:!active {
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6b9be8, stop: 1 #577fbf);
}
QTreeView::branch {
background: palette(base);
}
QTreeView::branch:has-siblings:!adjoins-item {
background: cyan;
}
QTreeView::branch:has-siblings:adjoins-item {
background: red;
}
QTreeView::branch:!has-children:!has-siblings:adjoins-item {
background: blue;
}
QTreeView::branch:closed:has-children:has-siblings {
background: pink;
}
QTreeView::branch:has-children:!has-siblings:closed {
background: gray;
}
QTreeView::branch:open:has-children:has-siblings {
background: magenta;
}
QTreeView::branch:open:has-children:!has-siblings {
background: green;
}
这是我的结果:
我想要一个这样的结果:
有人可以帮助我,提前谢谢。
答案 0 :(得分:9)
经过一些研究,我在Qt邮件列表中得到了答案,使用以下QStylesheet完成了所需的效果:
QTreeView {
background-color: tranparent;
selection-background-color: green; /* Used on Mac */
selection-color: white; /* Used on Mac */
show-decoration-selected: 1;
}
QTreeView::item:selected {
background-color: green; /* Used on Windows */
color: white;
}
改变风格的Mac系列,即使我在Windows上,我也不知道它是我的Windows版本中的错误,还是在这个Qt版本中。
答案 1 :(得分:0)
您只想让分支选择颜色与您的商品相匹配吗?
QTreeView::branch:selected {
background:
}
QTreeView::branch:selected:active {
background:
}
QTreeView::branch:selected:!active {
background:
}
等...