Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Unsolved QDockWidget default title not respecting setContentsMargins

    General and Desktop
    qdockwidget margins
    3
    5
    2326
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Erika
      Erika last edited by

      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:
      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.

      1 Reply Last reply Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by Chris Kawa

        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.

        Erika 1 Reply Last reply Reply Quote 0
        • kshegunov
          kshegunov Moderators last edited by

          @Erika
          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.

          Read and abide by the Qt Code of Conduct

          Erika 1 Reply Last reply Reply Quote 0
          • Erika
            Erika @kshegunov last edited by

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

            1 Reply Last reply Reply Quote 0
            • Erika
              Erika @Chris Kawa last edited by

              @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.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post