OJW OJW - 1 year ago 214
C++ Question

Qt padding & margins in dockable windows

In a Qt5.5 C++ program I have various QDockWidgets each containing one widget that [mostly] fills its QDockWidget. However, there's a 10px border around the widget which I can't get rid of.

Screenshot (extraneous space marked in red)

The following attempts didn't seem to achieve anything:





ui->dockable->setStyleSheet("margin: 0px; padding: 0px");

ui->dockable->setStyleSheet("QDockWidget{ border: 0px }");


To replicate the problem:

  • File -> new project -> Qt widgets application

  • Edit form mainwindow.ui

  • Drag dockwidget onto form

  • Drag other widget into dockwidget

  • Dockwidget background context menu -> Lay out -> vertically

  • Should give screenshot like above (in Windows 7)

Upgrading to Qt 5.7.0 doesn't help. Any ideas?

Answer Source

After you updated your post...

You must do:

ui->dockable->widget()->layout()->setContentsMargins( 0,0,0,0 );

ui->dockable->layout() is not the layout you need to modify. QDockWidget contains a QWidget (a kind of main widget, always available when QDockWidget gets created), this is the one where your layout was created in from QtDesigner, and so you must use QDockWidget::widget() to access it.

Alternatively, as you create the GUI from QtDesigner, you can also use this tool to remove your margins, it will make the GUI look like that:

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download