QDockWidget default title not respecting setContentsMargins
-
I've discovered that if you call setContentsMargins on a dock widget, the default title bar remains left aligned and at the fixed width, leaving unusually awkward spacing to the left. If the dockwidget is floating, this is a non-issue, with margins added as expected, however I'm not sure of the best way to resolve this when docked.
Simple example:
#include <QtWidgets\QApplication> #include <QtWidgets\QMainWindow> #include <QtWidgets\QDockWidget> int main(int argc, char *argv[]) { QApplication app(argc, argv); app.setApplicationName("Dock Margins Example"); QMainWindow mainWin; // setup widgets QDockWidget exampleDock(&mainWin); QWidget dockContents; dockContents.setStyleSheet(QStringLiteral("background: #FFFFFF;")); // contents colouring exampleDock.setWidget(&dockContents); mainWin.addDockWidget(Qt::LeftDockWidgetArea, &exampleDock); exampleDock.setWindowTitle("Example Dock"); exampleDock.setContentsMargins(30, 30, 30, 30); mainWin.show(); return app.exec(); }
Docked Issues:
Floating OK:
I've also noticed that the top margin appears ignored while docked (seems set as the same as title's height), but as-expected when floating.
Any suggestions on how to resolve the offset title (without having to create a custom title widget), or is it a bug? I've tried a few style changes, but nothing successful so far.
-
Looks like a bug. You should report it. The content margins feature has quite a few glitches. I've seen similar problems in other circumstances. It's not a very often used feature so these problems probably get overlooked.
As a workaround you can place a dummy widget and the real content inside it. You can then set the margins on the dummy or on its layout.
-
@Chris-Kawa Thanks. I agree it seems like a bug, and have now reported it: https://bugreports.qt.io/browse/QTBUG-51359. I figure the widget-inside-widget would be a workable alternative, but was hoping to avoid that route. C'est la vie.