c++ - qss中设置QTreeView选中项样式

标签 c++ qt qtstylesheets

我需要更改 QTreeView 上所选项目的背景颜色。 我已经尝试使用 this示例和 this堆栈溢出问题。

应用于::branch 子控件的样式工作正常,但::item 子控件中的命令均无效。

应用于 QTreeView 的 QSS:

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;
}

这是我的结果:

enter image description here

我想要这样的结果:

enter image description here

谁能帮帮我,在此先感谢

最佳答案

经过一些研究,我在 Qt 邮件列表中得到了答案。使用以下 QStylesheet 完成了预期的效果:

QTreeView {
    background-color: transparent;
    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 版本中的错误。

关于c++ - qss中设置QTreeView选中项样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34187874/

相关文章:

c++ - 使用享元模式在位图对象之间共享位图

C++类组合,为什么不调用构造函数?

c++ - 立方体的一半的 OpenGL 颜色

python - 向 pyqt 列表添加标题

c++ - 无法打开sqlite数据库

c++ - 如何使用 QSS 为禁用的按钮设置不同的背景颜色?

c++ - Opencv:如何获取然后更改 "Mat vector"的值

windows - clang 构建 qt 的 mkspecs 是什么?

qt - 无法从外部 QSS 样式表文件将边框设置为 QDockWidget

qt - QLineEdit 内容圆角的样式表?