Setting QDockWidget::title StyleSheet

hank picture hank · Jun 28, 2012 · Viewed 8.6k times · Source

My initial problem was to make QDockWidget title bold. I tried this and it worked:

myDock->setStyleSheet("QDockWidget { font: bold }");

But I can't understand why the following code doesn't work:

myDock->setStyleSheet("QDockWidget::title { font: bold }";

Even if I use more complicated style sheet, every parameter of it has effect except for font: bold:

myDock->setStyleSheet("QDockWidget::title { font: bold;
                                            text-align: left; 
                                            background: red; 
                                            padding-left: 30px; }");

What is the problem with QDockWidget::title font?

Answer

nayana picture nayana · Oct 29, 2014

First of all, I dont know why the font does not work, I can only quess. I have a felling that the default title bar is similar to window titlebar which is almost impossible to style. I was searching through source code which widget is used for title bar but found nothing.. Here is some code, good luck.

I think that the style sheet does not support font changes. By default for everything derived from QWidget applies that parameters like background etc will allways work. Other stuff like font may or may not be implemented.

But why wont you make custom title bar? It can be anything bundled in QWidget.

QLabel *label = new QLabel("Header Text", myDock);
label->setStyleSheet("color: orange; font-size: 14pt; font-weight: bold;");
myDock->setWidget(bodyWidget);
myDock->setTitleBarWidget(label);

I've tested with Qt 5.3 it works, although there are missing buttons like close or undock :-/

I think that you can create them (with push button or so) and bundle everything in one widget, then set it with setTitleBarWidget and connect some signals. There is at least hide() slot for close button and you may have to code slot for float using setFloat.