Setting padding and margin to 0 doesn´t work - qt

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);

Related

How can I set a QPushButton to grow vertically?

Background: I need Command Link-like controls. Normally, I would use Qt's builtin QCommandLinkButton, but in this case I need to also support Right-to-Left layouts. Regrettably, QCommandLinkButton doesn't seem to respect Qt::RightToLeft when set explicitly via setLayoutDirection. There also doesn't seem to be a way to override the layout direction via style sheets.
I tried to work around this issue by using QPushButton: I instantiated QPushButton and replaced its contents with a custom layout containing two labels stacked vertically. This did not work because QPushButton refused to expand horizontally.
Failing to make QPushButton instances work, I decided to try sub-classing it. No matter what I tried (overriding sizeHint, setting vertical sizePolicy to expanding, resizeing explicitly, etc) the button refuses to grow more than 30 pixels high.
If I change QPushButton to QWidget, the new widget grows normally.
The question: How can I force a QPushButton to grow vertically and fit its inner widgets? If this is not possible, what are my options for a button-like widget that is both visually and behaviorally consistent with QPushButton and other "native" controls?
I managed to get the button to grow normally by setting the maximum height to 16777214 (instead of the default 16777215), and then setting the content margins of the button to 10.
Perplexingly, the default maximum height of a QPushButton should be plenty, but for some reason it refuses to grow past 30 pixels unless the maximum height is explicitly set to something else.
Place the button inside a layout and set its vertical size policy to Maximum.

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.

Remove extra space around QLabel

I tried stylesheets with
padding:0px,
text-ident:0px;
margin:0px;
border: none;
And through code, changing the Size Policy to Expanding.
The layout inside the widget which has the label has
QHBoxLayout* lyt = new QHBoxLayout();
setLayout(lyt);
lyt->setContentsMargins(0,0,0,0);
And setting contents margins to 0 in the parent widget too.
The only way i got to make it smaller was setting Fixed size to less of the size. But the text has the word wrap property on, so it can be bigger and i cannot control that.
What should i do?
If you want to remove the extra spacing, then you can try the following on your layout. It worked for me.
lyt->setMargin( 0 );
lyt->setSpacing( 0 );
If you want to reduce the size of a particular QLabel, then you can set:
QLabel *myLabel = new QLabel( this );
myLabel->setMaximumWidth( MAX_WIDTH );
I already found out what was happening. The margins and all the stuff was working.
The problem was related to the borders. The top and bottom borders are 5 px height. If you set a border height, no matter which margins or padding you have, it will be a barrier for the contents. I´ll create another post to see what happens here, if there is another way to fix this, and override the positions of the borders.
Thanks!

Space between widgets in QVBoxLayout

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)

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