Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Widget background color at runtime



  • Qt 5.12.2, Linux, default GNOME desktop.

    I have

    QWidget with Grid Layout
        QLabel
        QGroupBox
        QGroupBox
        ...
        
    

    I set the top-level QWidget stylesheet to background-color: ....

    In Qt Designer this looks perfect --- the whole widget, its background, all its descendants are colored.

    However, at run-time it comes out with all the descendants (label, group boxes) themselves colored but not the background of the widget itself, between children (i.e. where the grid layout is). That background area remains "white" (or whatever the default window color is).

    What do I have to do? Is this some Linux/desktop flag/setting which comes into play at run-time to do with the window background color?? [At run-time this QWidget is being made a QMdiSubWindow, via QMdiArea::addSubWindow(QWidget), if that is relevant?]



  • @J-Hilk
    Dear @J-Hilk,

    At the time I accepted your suggestion that my problem was a Qt 5.12.2 issue, as per the bug report. I worked around it by setting the color on the conveniently-available parent widget.

    However, I now discover that my problem is as per my post at https://forum.qt.io/topic/115640/how-to-see-the-area-detected-by-qmouseevent/9, i.e. I need to add:

    myDerivedWidget->setAttribute(Qt::WA_StyledBackground, true);
    

    and then the background color works at run-time.

    Your bug report may well apply, but I don't think it's my case here. I trust you will understand if I now mark this post as the solution to my issue here!


  • Moderators

    @JonB

    thats a bug in Qt, you have 3 options:

    • upgrade to 5.12.6/7
    • only set stylesheets in designer
    • only set stylesheets in code

    Edit:
    found the related bug report:
    https://bugreports.qt.io/browse/QTBUG-79545



  • @J-Hilk
    Thank you. I was mixing design- & run-time stylesheets here, and was about to mark yours as solution, but it seems to happen to me with design-time only too.

    On a hunch, after QMdiArea::addSubWindow(QWidget) I retrieve QWidget::parentWidget() (which I think is something like a viewport to do with MDI window) and I set its stylesheet to have the background color there. Lo & behold, the whole widget is now colored! So at least in my case I am marking this as solution, whether or not your bug applies, though thank you very much.



  • @J-Hilk
    Dear @J-Hilk,

    At the time I accepted your suggestion that my problem was a Qt 5.12.2 issue, as per the bug report. I worked around it by setting the color on the conveniently-available parent widget.

    However, I now discover that my problem is as per my post at https://forum.qt.io/topic/115640/how-to-see-the-area-detected-by-qmouseevent/9, i.e. I need to add:

    myDerivedWidget->setAttribute(Qt::WA_StyledBackground, true);
    

    and then the background color works at run-time.

    Your bug report may well apply, but I don't think it's my case here. I trust you will understand if I now mark this post as the solution to my issue here!


Log in to reply