问题
I have a problem with making contents of QScrollArea to not draw a background.
So here is the initial picture, when I apply no stylesheet:
You can see, that the contents of scroll area are darker then the overall frame.
I have found the stylesheet that I can apply to scroll area, so the background is transparent. Here's the stylesheet I use:
QScrollArea { background: transparent; }
QScrollArea > QWidget > QWidget { background: transparent; }
Problem is that when I do that, the scrollbar of scrollarea gets messed up:
You see? The scrollbar is now always visible and is ugly and transparent.
How can I make my stylesheet not to affect the scrollbar, while still applying it to the QScrollArea? Or what different stylesheet should I apply instead?
回答1:
try to set an object name for the scroll area viewport:
pScrollArea->viewport()->setObjectName("myViewport");
then address it using the hashtag property in the stylesheet (also add the groupbox since the way I see it in your screenshot, your goal is to make them transparent as well):
QScrollArea, #myViewport, QGroupBox {
background: transparent;
}
回答2:
The problem is that QScrollBar is a subclass of QWidget, so just target the viewport with that rule:
pScrollArea->setStyleSheet("QScrollArea { background: transparent; }");
pScrollArea->viewport()->setStyleSheet(".QWidget { background: transparent; }");
Notice the dot before QWidget, so to not target any children of the viewport (i.e., any viewport content that's a QWidget subclass).
来源:https://stackoverflow.com/questions/25795112/qscrollarea-transparent-background-on-macos-x