Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt remove empty space between widgets on QVBoxLayout

I have annoying problem. I created QVBoxLayout on which I added my widgets. This is sample from my constructor:

layout = new QVBoxLayout;
layout->setMargin(0);
layout->setContentsMargins(QMargins(0,0,0,0));
layout->setSpacing(0);

And then I have function to add widgets.

layout->addWidget(_wave);

_wave is my own widget. But you can add whatever you want, for example QButton.

What do I want achieve? Similar like this but without any spaces beetween widgets added to layout. Just only QButtons or other widget, sticked each other.

I added everywhere setMargins, setSpacing etc. Please help me with that, I don't really have an idea what should I do.

enter image description here

Sorry for colors, but I wanted to mentioned what I want to achieve. I have mainWindow on which I added QWidget. This widget have blue background. Then to the layout, Im addding some widgets, which are orange on this image. I just want to be sure, that this blue background between widget isnt visible. I want to have widget under widget, without any space.

like image 891
Tatarinho Avatar asked Jan 20 '15 18:01

Tatarinho


2 Answers

I know the question was posted a year ago, but if it can help some people save some time, I would be glad. I had the same problem and was able to solve it by setting:

  • QFrame.setFrameShape() to NoFrame
  • QFrame.setLineWidth() to 0.

The image below is the result of 2 QtextEdit put side by side in a Layout, using the described method.

enter image description here

http://doc.qt.io/qt-5/qframe.html#lineWidth-prop

like image 175
BaldDude Avatar answered Nov 12 '22 10:11

BaldDude


Try this to achieve a "tight" look. Note that if you resize the parent widget, they will not move. Not sure if this is what you want or not, but...

// After all widgets are added to the layout
layout->insertStretch( -1, 1 );

The 2nd argument needs to be higher than any other stretch factor. Stretch factor is default zero, so if you don't set it, the above one-liner should work.

like image 3
kiss-o-matic Avatar answered Nov 12 '22 10:11

kiss-o-matic