qtqscrollareaqt5.3

QScrollArea clears background color


In my qss style sheet I define a background color

#sw_MainMiddle {
    background: black;
}

Everything fine, but when I place a scroll QScrollArea in the sw_MainMiddle widget, the background color is gone. Obviously there is no chance to assign a background directly to QScrollArea. Below code has no effect:

QScrollArea {
    background: black;
}

According to this question I have used object name selector to re-assign the background color to the scroll area, no effect too:

#myScrollArea {
    background: black;
}

Anything I am doing wrong?


Solution

  • I think I found the solution:

    QAbstractScrollArea #scrollAreaWidgetContents {
        background-color: black;
    }
    

    where scrollAreaWidgetContents is the widget inside the scroll area:

    enter image description here

    From the QSS Reference Page:

    All derivatives of QAbstractScrollArea, including QTextEdit, and QAbstractItemView (all item view classes), support scrollable backgrounds using background-attachment. Setting the background-attachment to fixed provides a background-image that does not scroll with the viewport. Setting the background-attachment to scroll, scrolls the background-image when the scroll bars move.

    See Customizing QAbstractScrollArea for an example.

    So you can't customize let's say all QAbstractScrollAreas or all QScrollAreas, only the widgets containing them(e.g. QTextEdit). That's why QScrollArea { background-color: black; } doesn't work. However if you want to customize particular widgets containing scroll area this solution should be sufficient.