Remove extra space around QLabel - qt

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!

Related

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.

How to get a Borderlayout to fill its container?

I am a GWT programmer trying to get to grips with using PlayN with Tripleplays gui library.
Having a little bit of trouble working out how to get a border layout filling up all the space of its container. (presumably with the middle space expanding to fill the available size)
BorderLayout border = new BorderLayout(3);
Group mainLayout = new Group(border);
mainLayout.setConstraint(AxisLayout.stretched());
Button Center= new Button("test");
Center.setConstraint(BorderLayout.CENTER);
mainLayout.add(Top);
mainLayout.add(Bottom);
mainLayout.add(Left);
mainLayout.add(Center);
Top Bottom and Left are similarly specified to BorderLayout.Left, top etc.
the whole thing is then added to the root screen with
_root.setConstraint(AxisLayout.stretched());
_root.add(0, mainLayout);
(_root itself just has a AxisLayout.vertical() ...which I am assuming makes it act somewhat like a gwt vertical panel)
At the moment the vertical space seems to be filled, but not the horizontal.
Any ideas where I am going wrong?
I would suggest to set
border.setConstraint(AxisLayout.stretched());
before adding border to mainLayout. This will tell mainLayout to strech this child widget.

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

How to make a Qt widget change its size?

My problem probably is very simple, but I have no idea how to solve it.
So, I have several widgets in vertical layout. Some of them in some moment should be invisible. I suppose this moment the control that has expanding vertical policy should increase its height but it never happens. Why? How to force it to change the size? So far there is only one way to do that - to change the size of window manually a little bit and only after it the widget changes its height.
When you make a widget invisible (i.e. call hide), also remove it from the layout (see removeWidget). That way all of the other widgets will automatically resize to make use of the extra space. Since you are only hiding the widget, its space is still reserved in the vertical layout.
Make use of insertWidget to maintain the layout ordering when transitioning a hidden widget back into view. Otherwise, addWidget would always place it at the bottom. This approach should be less complex than managing the sizes manually.
Try calling adjustSize() on the parent of the layout.
Have you tried setting the stretch on 1 for the widget you want to be expanded when others are hidden?
QBoxLayout::addWidget( my_widget, 1 );
When this widget is added with a stretch of 1 and the other widgets without stretch, this widget will expand to the available space. Maybe that will do the trick.

Flex scrollbar styling issue

I'm trying to style vscrollbar and hscrollbar inside a Vbox.But there's always a white square thing at the right bottom cornor which can not be styled.
My CSS is:
ScrollBar{
downArrowUpSkin: Embed(source="assets/images/scrollbar/arrow_down.png");
downArrowOverSkin: Embed(source="assets/images/scrollbar/arrow_down.png");
downArrowDownSkin: Embed(source="assets/images/scrollbar/arrow_down.png");
upArrowUpSkin: Embed(source="assets/images/scrollbar/arrow_up.png");
upArrowOverSkin: Embed(source="assets/images/scrollbar/arrow_up.png");
upArrowDownSkin: Embed(source="assets/images/scrollbar/arrow_up.png");
thumbDownSkin: Embed(source="assets/images/scrollbar/thumb.png");
thumbUpSkin: Embed(source="assets/images/scrollbar/thumb.png");
thumbOverSkin: Embed(source="assets/images/scrollbar/thumb.png");
trackSkin:Embed(source="assets/images/scrollbar/track.png");
fillAlphas:0,0,0,0;}
Could anyone help me out?Much Thanks!
This is a weird one. The white box at the bottom right is actually a (raw) child of the container.
To get around this you need to subclass whatever container you want to add your styled scrollbars to and remove the child called "whitebox":
var whitebox:DisplayObject = rawChildren.getChildByName('whiteBox');
if (whitebox)
rawChildren.removeChild(whitebox);
IIRC you need to do the above in two places: an override of createChildren and an override of validateDisplayList. In both cases remember to call the super class method first!
That area isn't controlled by the scroll bar(s), it's part of the original container. Does the VBox have it's background colour set to black?

Resources