其他分享
首页 > 其他分享> > c – 在qss中设置QTreeView选择的项目样式

c – 在qss中设置QTreeView选择的项目样式

作者:互联网

我需要在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;
}

在这里我的结果:

enter image description here

我想要一个这样的结果:

enter image description here

有人可以帮助我,提前谢谢.

解决方法:

经过一些研究,我在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版本中.

标签:qtstylesheets,c,qt
来源: https://codeday.me/bug/20190722/1503689.html