OJW OJW - 15 days ago 5
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->layout()->setMargin(0);

ui->dockable->layout()->setSpacing(0);

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

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

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

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

this->layout()->setContentsMargins(0,0,0,0);


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

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

Comments