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

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

Related

How to restrict growth (size) of layout in Qt Designer?

When I maximize my window, I want to restrict a vertical layout (and the entire row below it also) so that it has a specific size (lets say a width of 200). How can I achieve this? Below is what I get now. The buttons are stretched too far. I want them to keep a width of 200.
To stop the buttons stretching, use the following steps in Qt Designer:
click on scrollArea in the Object Inspector
click on Break Layout on the toolbar
click on scrollArea in the Object Inspector
click on Lay Out in a Grid on the toolbar
click on scrollAreaWidgetContents in the Object Inspector
scroll down to the bottom of the Property Editor
change layoutColumnStretch to 0,1
These steps should remove an empty column from the scroll-area grid-layout, and make the second column stretch to take up the available space when the window is resized.
You just need to restrict the maximum width of all widgets (in this case the buttons) within the layouts of this grid column to the expected size, else they'll just keep expanding. You may also have to fiddle the horizontal size policy; I seem to remember that buttons were a bit tricky in this regard (or was that the height?), but can't test it right now.
The layout size contraint you tried only applies to the layout's direct parent widget, if it has one, which isn't the case for the vertical layouts here.

QScrollArea - how to do precise programmatic scrolling

I am using Qt to build a view for multipage documents. I'm drawing each page to a separate QLabel widget, like in the ImageViewer example app.
The QLabels are organized vertically using QVBoxLayout. This all works nicely, with a little grey margin between the pages.
What I want now is, when the user does page down, to move the scroll so that the top of a particular QLabel appears right at the top of the window. the "ensure" functions might do that, but I'm not immediately seeing how.
Has anyone done something like this?
If a child widget is taller that the viewport height ensureWidgetVisible scrolls to the middle of the widget.
If you need to scroll to the top of the widget you can do it easily with a little calculation:
//childWidget - QLabel you want to move to
//area - QScrollArea
// calculate childWidget position in coordinates of the viewport
const QPoint p = childWidget->mapTo(area, QPoint(0,0));
// move scroll bar
area->verticalScrollBar()->setValue(p.y() + area->verticalScrollBar()->value());

How to set the desired size of a QMainWindow with QScrollArea?

I am working on a project where I have to display a pretty large (vertically) main Widget.
In the initial Version of my GUI it was just added as the central Widget of a QMainWindow, which caused the Problem that on small screen resolutions the controls on the Bottom of the Widget are unreachable.
To solve this i wrapped a QScrollArea around the main Widget, but now the main window is always relatively small even if it doesn't have to.
What do i need to change so the Main Windows (vertical) size is large enough to show all the contents unless it would be too large for the screen resolution? Also I don't want it to be stretched, so simply always using the whole vertical screen resolution is not an option. Ideally the size should be fixed to the size needed by the contents (w/o the scroll area) and only smaller where needed.
Overriding the sizeHint method did only resulted in a small enlargement of the Window and setting the minimal height brings me back to the beginning where some of the controls are not assessable on small resolutions.
Since i am new to QT I am actually out of ideas how to google the solution because most Solutions I can find are about sizing components inside a Window and not the Window itself.
By default a QScrollArea will not attempt to expand to fit its contents. In order to do this you will need to re-implement QScrollArea's sizeHint() to return the size of QScrollArea's child widgets.
In your question it sounds like you were trying to re-implement MainWindow's sizeHint? re-implementing sizeHint on the top-level window will have no effect as sizeHint designed for use with widgets inside layouts.

Ajust QVTKWidget to window

How can I make my QVTKwidget to adjust when I make my window expand or shrink?
I have more widgets in my layout. I want them to stay the same, just expand shrink the QvtkWidget. I found some things about the layout, but I can't use it on QVTKWidget.
Assuming this is a reasonably well behaved widget, you could either use:
QMainWindow and set the widget as the central widget (setCentralWidget)
Use layouts such as QVBoxLayout and ensure you set the stretch parameter of your QVTKWidget widget to 1 and your other widgets to 0, see QBoxLayout::addWidget.

QT Layout - initial directions

I am new to QT. I'm trying to understand the layout mechanism by trying to implement this small window seen below. It has the following elements under the QWidget that's the main window:
One big QWidget that stretches on all the client area.
Two QWidget containers on the top of the window. Both should have the same height, but the right one stretches horizontally, as the window grows/shrinks.
one button container widget on the top right, with fixed height and width
Large QWidget container filling the rest of the client area, that should resize as the window resizes.
The parent window itself is resizeable.
I'm looking for hints as to what layout I should use. How do I achieve this programatically? define what stretches automatically, what stays with a fix size? and how the proportions are kept where they need to be kept.
I'd appreciate any pointer you may have.
The easiest, and IMHO best, way to accomplish this is via the QHBoxLayout and QVBoxLayouts. You can do this via the designer in QtCreator, but I find it doesn't work perfectly if you need to adapt things over time. If it's a static set of widgets, I do suggest designing it using the QtCreator designer as it'll greatly simplify your life.
If you're going to do it programatically, the main window should be set to use a QVBoxLayout and then two sub-QVBoxLayout's after that, where the bottom one is configured to take any space it can get. Then in the top QVBoxLayout, add a QHBoxLayout with your two upper components.
to set a widget to fixed size in code you call setFixedSize( int h, int w ) on the widget. To do it in Designer click on the widget and look in the property editor in the QWidget section. open the sizePolicy thingy and set horizontal and/or vertical to fixed. Then open Geometry and set the width and Height.
To make them stretch at different ratios in code you use a separate argument when using a box layout. eg layout->addWidget( button1, 1 ); layout->addWidget (button2, 2); this would cause button2 to expand at twice the rate of button1. To do this in designer, open the sizePolicy property of the widgets and set the HorizontalStrech and/or VerticalSretch. Note that the size policy needs to not be Fixed in this case for the direction you want to set the stretch on. Also it will never let a widget shrink below its minimum size (it would rather mess up the ratio than shrink something too small).

Resources