OJW
OJW

Reputation: 4632

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:

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

Upvotes: 1

Views: 3677

Answers (2)

jpo38
jpo38

Reputation: 21544

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

Upvotes: 2

jpo38
jpo38

Reputation: 21544

Would be easier to help if you sent some pictures of what you have and want.

Wrote this piece of code as a sample (executed in a QMainWindow):

QWidget* centralWidget = new QWidget( this );
centralWidget->setStyleSheet( "background: blue");
setCentralWidget( centralWidget );

QDockWidget* dock1 = new QDockWidget( "docking bar1", this );
QWidget* widget1 = new QWidget( dock1 );
dock1->setWidget( widget1 );
widget1->setStyleSheet( "background: red");

QDockWidget* dock2 = new QDockWidget( "docking bar2", this );
QWidget* widget2 = new QWidget( dock2 );
dock2->setWidget( widget2 );
widget2->setStyleSheet( "background: green");

addDockWidget(Qt::LeftDockWidgetArea, dock1);
addDockWidget(Qt::RightDockWidgetArea, dock2);

blue central widget + 2 docking bars, one filled in red, the other in green (makes it easy to see the borders: they remain grey areas).

It shows like that here (Mint OS) (intentionally undocked the green docking bar):

enter image description here

See the green undocked docking bar grey borders. Now, apply it this stylesheet:

dock2->setStyleSheet( "QDockWidget { \
                             border: 0px \
                                   }" );

Then you get no border anymore:

enter image description here

But I'm not sure that will fix your problem. You should post screenshots because docking bar looking really depends on the OS. I see no margin when the docking bar is docked on my OS, but you probably see one (note that the grey vertical line between the red and blue area in my screenshots is not a margin but a slider that can be used to resize the docking bar, I don't believe you are trying to suppress this).

Upvotes: 1

Related Questions