Space between widgets in QVBoxLayout - qt

I'm trying to make a sign-in form with Qt5.0.1. I created 2 Widgets and I put my labels and line edits in one of them, and my button in other one. Then I put these 2 widgets in a QVBoxLayout, but the space between two widget in layout is more than purpose. I tried setSpacing and setContentsMargin but they didn't work for this program.
vhandle->setSpacing(0);
vhandle->setMargin(0);
vhandle->setContentsMargins(0,0,0,0);
vhandle->addWidget(handle,0, Qt::AlignTop);
vhandle->addWidget(handle2,0, Qt::AlignTop);
but nothing changed in space between two widgets:
what should I do?

The issue is not the margin settings of the QVboxLayout, but the margin settings of the layout of your container widgets and the spacing setting of the QVBoxLayout. You already have set the spacing to 0, this should be fine. In addition, assumed that upperWidgetLayout is the layout of the upper widget and lowerWidgetLayout the layout of the lower widget, try
upperWidgetLayout->setContentsMargins(-1, -1, -1, 0);
lowerWidgetLayout->setContentsMargin(-1, 0, -1, -1);
This sets the bottom margin of the upper widget's layout and the top margin of the lower widget's layout to 0, so that there is no space between the contents of the two widgets:
Temporarily coloring the various widgets is usually a good approach to track down such issues. You can also use Qt Designer to design the UI and have a look at the source code which is being generated (or use the .ui file directly in your project).

The extra space between widgets or layout can be removed by setting the alignment at top here is an example:
self.layout_scrollarea_v=QVBoxLayout(self.frame)
self.layout_scrollarea_v.setAlignment(Qt.AlignTop)
self.layout_scrollarea_v.addLayout(self.layout_scrollarea_h1)
self.layout_scrollarea_v.addLayout(self.layout_scrollarea_h3)
self.layout_scrollarea_v.addLayout(self.layout_scrollarea_h2)
in your code use
vhandle->setAlignment(Qt::AlignTop)

Related

How to drag multiple layouts in a layout graphically in Qt Designer?

I'm using Qt Designer to create a graphic interface, but i am struggling to do a basic operation.
I want to create a vertical layout in which there are 2 (or more) horizontals layouts. But if I try to grab my horizontal layouts to put them in the vertical one, the first horizontal layout ends up taking all the space in vertical layout, so i can't add the second one other than inside the first hozitontal layout.
I end up with Horizontal layout 1 in Horizontal layout 2 in Vertical layout which is not what i want
here is the picture or what i get :
VS what i want :
I tried to do it through the Tree object at the right, but it seems we can't drag and drop objets there nor can i copy and paste it to change the ancestor. Is there any way to do it other than editing the XML file ?
One fewer horizontal layout (spacers added for clarity):
Cause
Qt Designer sets the margins of the newly added layouts to 0, 0, 0, 0, so it is impossible to target with the mouse a 0px-wide area when you try to add something to the layout.
Solution
Change the margins of the layout temporarily to something, which is comfortable for you to hit with the mouse. Then you can easily drag and drop new items to this layout. Later you could change the margins back to 0.

Strange behavior of margins for Qt controls

I have just started using Qt (5.3) and encountered the fact that some controls appears with margins which I cannot control.
First, I tried to put QLabel and QPushButton right inside QMainWindow
window = new QMainWindow;
label = new QLabel( title, window );
In this case label appears with a margin of 12 pixels at the top (see picture).
QPushButton appears with 1 pixel top & left margins.
But if I insert QFrame with a border, it appears without any margin.
So the margins seem to be attributes of QLabel and QPushButton.
BUT:
When I tried to add extra QFrame between windows and controls:
window = new QMainWindow;
frame = new QFrame(window );
label = new QLabel( title, frame);
I had got different picture:
QLabels top margin had shortened to 1 pixel
QPushButton 1 pixel margins remained intact, but the height of the button had changed
I have tried:
setStyleSheet( "padding:0px" )
and
setContentsMargins( 0, 0, 0, 0 )
for all elements, but without any success.
Any help would be greatly appreciated
The QMainWindow class isn't designed to have widgets added to it directly. Whatever results you see are due to this fact.
The "margins" that you see are not really margins. Since a QLabel is a QFrame, you can enable its frame to see that it has no margin - merely the text is offset from the edge, and that's by design. You can similarly overlay a same-size translucent rectangle on a QPushButton to see that there is also no margin, merely the styling adds its own platform-specific margin. Do not mistake the platform styling mechanism for the style sheets: they are two separate mechanisms and mostly exclusive, with the use of the latter disabling the effects of the former, with few exceptions. For example, the stylesheet spacing/margins/padding is additive to whatever the platform style mandates.
See this answer for an example of how to show overlays on any widget without subclassing.

Qt & Qt Designer - Making widget fill the parent without padding

I wanted to make my widget fill the parent window, even when the window resizes, so I read this: How to make a Qt Widget grow with the window size?
But this solution created a new problem: my widget automatically re-sizes to the size of the window, but there's padding on the sides of the window. I want the widget to completely fill the parent, and it's not doing that. Look:
Here you can see that the tab widget doesn't entirely fill the parent. I've done some research and have seen that through programming, you can configure the layout to get rid of this padding. Problem is, I'm building my GUI in QDesigner, so I can't just go layout->setMargin(0);
My question is, how to I get rid of this padding on the sides of my window through Qt Designer?
In the bottom of central widget properties there is a section of Layout (it is red), where you can set layout margins. Also, you still can do it programmatically:
QMainWindow::centralWidget()->layout()->setContentsMargins(0, 0, 0, 0);

Setting padding and margin to 0 doesn´t work

It´s just a container and i want to put widgets inside and hide them and show them. I don´t want it to have any margins or paddings, and it will be invisible (no border, no background)
I set the QWidget#container to margin:0px, padding:0px through a stylesheet.
And setObjectName("container") to all the widgets that contain.
Nothing happens. But setting a background color works, so it is executed.
In which cases does this happen?
How to fix this?
QWidget does not support box model, so it does not understand padding/margin CSS directives. Use QFrame as container. To see which widgets support box model take a look at list of stylable widgets
Given there was no concise answer I'll sum up the above:
To create a container with no margins and padding, instead of QWidget, use QFrame, and set a layout on it. Then set the spacing to 0 and set the content margins to 0 as well on the layout. Using a stylesheet setting padding/margin to 0 will have no effect.
Code:
QFrame* containerFrame = new QFrame();
QVBoxLayout* layout = new QVBoxLayout;
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
// add some widgets to the layout
containerFrame->setLayout(layout);

Problem with a Qt layout, I can not remove a space

Good morning,
I have to layout some QWidgets and layouts into a main layout, but I have a problem with a space that I can not remove.
Basically what I would achieve is a horizzontal layout containing a grid layout and some buttons ( all in a horizontal line ). The grid layout (2x2) contains 2 QLabels and 2 QLeds.
Unfortunately Qt place a space between the grid layout and the first button as you can see in the attached image here http://img413.imageshack.us/img413/9132/problemhu.png
I would remove such space.
Here the code I wrote:
QGridLayout* gl = new QGridLayout();
gl->setAlignment(Qt::AlignLeft);
gl->setContentsMargins(0, 0, 0, 0);
gl->addWidget(activeLabel, 0, 0);
gl->addWidget(m_focusLed, 0, 1);
gl->addWidget(encodingLabel, 1, 0);
gl->addWidget(m_encodingLed, 1, 1);
This created the grid layout and added the QLabels and QLeds on it.
Then I add the buttons into the horizontal layout so:
/* layout buttons */
QHBoxLayout* lo = new QHBoxLayout();
lo->setSpacing(0);
lo->addLayout(gl); // <--here I add the grid layout
lo->addWidget(m_goToBeginBtn);
lo->addWidget(m_goToEndBtn);
lo->addWidget(m_frewBtn);
lo->addWidget(m_fforBtn);
lo->addSpacing( 10 );
lo->addWidget(m_ffrewBtn);
lo->addWidget(m_ffforBtn);
lo->addSpacing(10);
lo->addWidget(m_prevBtn);
lo->addWidget(m_nextBtn);
lo->addWidget(m_playBtn);
lo->addWidget(m_stopBtn);
lo->addWidget(m_cutBtn);
lo->addSpacing(10);
lo->addWidget(m_zoomInBtn);
lo->addWidget(m_zoomOutBtn);
lo->addSpacing(10);
lo->addWidget(m_bgSndCheckBox);
lo->addWidget( m_showPanelBtn);
I don't know why Qt place such space between the grid layout and the first button. I would remove it. How can I do? I didn't get help from the Qt mailing list.
Best Regards
How to fix this largely depends on what behavior you want to see. I'm guessing what you want is for the labels and the Leds to stay exactly where they are, and keep their size.
What is happening is that the grid layout is resizing with your window, (like your buttons), but the left alignment is keeping the controls stuck to the left, thus the space.
First, remove the gl->setAlignment(Qt::AlignLeft) line.
Secondly, you want to make sure you set the sizePolicy properly on both your QLabels and your QLeds, because otherwise your QLeds will start to resize horizontally. What you want is a fixed horizontal size policy. Here is an example:
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
label->setSizePolicy(sizePolicy);
A completely different way to accomplish the same thing would be to add a bunch of calls to setStretch() on your horizontal layout. But you would have to do this for each column in your layout that you want to stretch. Basically each for each button, but skipping the grid layout in the first column. Like this...
lo->setStretch(1, 1); // Column 1 is your first button
lo->setStretch(2, 1);
...
lo->setStretch(19, 1)l // 19 columns in total, 15 buttons plus 4 spacing.

Resources