Congratulations to our 2022 Qt Champions!

Scroll bar of QTextBrowser loses colors after focussing?

  • I have set the stylesheet of my QTextBrowser widget to background-color:white. autoFillBackground is set to true. It is the child widget of a QFrame and placed in a grid layout so that all but the frame's margin (approx. 2 pixels) is covered. When the text browser has the focus, the background color of the QFrame is changed to green by setting the stylesheet property; when it loses focus, it is reset to the normal light-gray color.

    When the main window containing these widgets is first opened, the scroll bars of the text browser are shown with their normal colors (see screenshot attached:
    scroll_bars_normal.png: scroll_bars_normal.png

    But after the browser is focussed once, they turn white, even though I have not changed the stylesheet of the text browser (see screenshot

    What is going on here?

  • Lifetime Qt Champion

    And you used the QTextBrowser as selector in the stylesheet and not
    QWidget or similar ?

  • @mrjj I set the background-color for the QFrame widget (which is the parent of the text browser) when the text browser acquires the focus. But according to the documentation, stylesheets should not propagate to the children, as palettes do (if I understand this correctly, which I probably don't). And the text browser has its own style sheet which also merely has background-color:white; as the content.

  • Lifetime Qt Champion

    Oh but stylesheets do propagate.
    If the selector matches, then any children will be affected.
    Just try
    QWidget {
    on a parent and all children will be colored.

  • @mrjj Thank you, this is stated very clearly at the top of the documentation about "Qt Style Sheets", but I seem to have overlooked it.

    The problem was that I had set some things in Qt Designer and weird things were happening with the palettes as well. So I deleted all of the style sheets I had set and made sure that the palette properties all said "inherited".

    Now everything is working properly!

  • Lifetime Qt Champion

    Often one stylesheet is used and only set on the application.
    So it affects all it children and much easier to manage as its just one sheet.
    Mixing palette and stylesheet can also sometimes do strange stuff.
    At least it did for me.

Log in to reply