I have a QDockWidget
:
I would like to alert the user to certain events by setting the background color of the title bar.
I have achieved this by setting the style sheet for my DockWidget
:
void DockWidget::setCriticalAlert()
{
setStyleSheet("QDockWidget { background-color:red; }");
}
The result is this:
The problem is that the background-color
doesn't get applied when the QDockWidget
is docked:
How can I get the background color to be applied when the QDockWidget
is docked?
This is a bug in Qt.
Issue 10537
Quoting from the linked issue:
The problem is that in QDockWidget::paintEvent, there is a isFloating() condition before drawing PE_FrameDockWidget. We cannot jsut remove this condition as it would break the other style (that does not whish to draw frame when the dockwidget is docked) We cannot either use PE_Widget to draw the frame as then it goes over the dockwidget's title The solution is maybe to introduce a new PE_FrameDockWidgetDocked primitive element. Or some SH_DockWidget_DrawDockedFrame stylehint to draw the frame in every cases.
a valid workaround seems to be to set the stylesheet of the parent, and use the class-and-id selector. Forgive the python formatted code but the concept is the same - in this case, 'dock' is a QDockWidget which has been given an object name using setObjectName(), and its parent, the QMainWindow, is 'self':
self.setStyleSheet("QDockWidget#"+str(dock.objectName())+"::title {background-color:red}")
In PyQt5.5, this works at runtime, i.e., can be changed on the fly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With