Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QDockWidget::background-color not applied when docked

Tags:

c++

qt

I have a QDockWidget:

enter image description here

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:

enter image description here

The problem is that the background-color doesn't get applied when the QDockWidget is docked:

enter image description here

How can I get the background color to be applied when the QDockWidget is docked?

like image 577
Steve Lorimer Avatar asked Oct 27 '15 21:10

Steve Lorimer


2 Answers

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.

like image 98
Steve Lorimer Avatar answered Sep 20 '22 21:09

Steve Lorimer


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.

like image 40
Tom Grundy Avatar answered Sep 19 '22 21:09

Tom Grundy