@Bonnie, @luciole
Well blow me down! This is indeed the problem/solution in my own code!
Please see my recent report of this issue in https://forum.qt.io/topic/113109/widget-background-color-at-runtime. There I have a QWidget-derived class, whose background I wish to set, which is the child widget of a QMdiSubWindow. It worked in Designer but not at runtime.
At the time I accepted @J-Hilk's reply at https://forum.qt.io/topic/113109/widget-background-color-at-runtime/2
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
Maybe there is a Qt 5.12.2 issue, but it does not seem to be my case.
Until now I had "solved" this by placing colored background not on my subclassed widget but instead on its parent, which is a QMdiSubWindow.
Instead, I now find that all I have to do is add
myDerivedWidget->setAttribute(Qt::WA_StyledBackground, true);
and the background color works at run-time!
FWIW, I tested and I do not have to have the Q_OBJECT macro on the derived class to make this work, though it does no harm. I do have to set the attribute.