Search code examples
c++qtqtstylesheets

Setting QTreeView selected item style in qss


I need to change the background color of the selected item on a QTreeView. I already tried using this example and this Stack Overflow question.

The style applied to the ::branch subcontrol works fine, but none of the commands in the ::item subcontrols works.

The QSS applied to the 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;
}

And here my result:

enter image description here

I want a result like this:

enter image description here

Can someone help me, thanks in advance.


Solution

  • After some research, I got the answer in the Qt mailing list. The desired effect was done with the following 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;
    }
    

    The Mac lines that change the style, even if I'm on Windows, I don't know if it is a bug in my windows version, or in this Qt version maybe.