Important: Please read the Qt Code of Conduct -

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
    	mainWin.addDockWidget(Qt::LeftDockWidgetArea, &exampleDock);
    	exampleDock.setWindowTitle("Example Dock");
    	exampleDock.setContentsMargins(30, 30, 30, 30);
    	return app.exec();

    Docked Issues:
    alt text
    Floating OK:
    alt text

    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.

  • Moderators

    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.

  • Moderators

    Instead of this:

    exampleDock.setContentsMargins(30, 30, 30, 30);

    You could try something along the lines of:

    exampleDock.layout()->setContentsMargins(30, 30, 30, 30);

    Although, I'm not quite sure it'll work.

    Kind regards.

  • @kshegunov Thanks. Unfortunately that doesn't have any impact.

  • @Chris-Kawa Thanks. I agree it seems like a bug, and have now reported it: I figure the widget-inside-widget would be a workable alternative, but was hoping to avoid that route. C'est la vie.

Log in to reply