Qt: How to fit content to QScrollArea - qt

I reimplement a QScrollArea and I want to add several widgets to it at runtime. The problem is, till the scrollbars are shown the content of the QScrollArea doesn't fit to it. Only if I add more widgets so the scrollbars shown the content fits correctly.
I already tried this after adding widgets
this->widget()->resize(this->widget()->sizeHint());
or
this->widget()->adjustSize();
But this doesn't worked. What I have to do to resize the content? Why the content fits after the scrollbars appear?

Add an appropriate layout to your scroll area before setting a widget.
Set the size constraints(min and max property ) of the widgets being added
I am not sure about your widget layout inside the scroll area, but you can make use of spacers to align the widgets ( when initially there are not many widgets to fill the scroll area for the scroll bar to appear)

Related

Qt: Why children widgets overlapping in QGridLayout?

I have an widget x with fixed size. Then at first I took a QScrollArea and then a QGridLayout which I set as the layout of scrollAreaWidgetContent. Then I started adding some widget x in the layout at (0,0), (0,1), (1,0), (1,1)... of grid layout. Then when I ran the program, strangely when I shrink the window vertically, the widgets overlap.
And as expected, when I increase the window size vertically, they are not overlapped anymore.
But strange thing is this problem does not occurring when I shrink window horizontally. For example,
My question is, why this is happening and more importantly, keeping in mind I want to use gridlayout, how to solve this problem?
As you said in your question:
Your widget has a fixed size, so the layout does not shrink them when there is no space left.
So the question is: what do you want to happen when you shrink the window?
If you want to shrink your widget you have to change the sizePolicy of your widgets from "Fixed" to "Preferred".
If you want to reduce the number of widgets in the layout, then you could add an event handler in the resize event and remove them
If you want to disallow the shrinking of the window, then you need to set the sizePolicy of the scrollarea to a fixed or minimumSize

Orphan QScrollArea remains after adding to layout

I'm having trouble adding a QScrollArea to a layout within a QDialog widget. When I add the scroll area to the layout, it appears where I want it with the widget I've assigned to it and it scrolls just fine. However, I am also left with a blank scroll area stuck to the top left of my dialog, as if I hadn't added the scroll area to the layout at all!
Why is this happening?

Qt Layout Flexible Grid

I have a bunch of buttons in Qt with a fixed size. Now I want to create a layout in which they are shown in a grid. I want the grid to be scrollable and when I resize my mainwindow, the grid should fit as much buttons in the layout as possible. The number of rows and columns are therefore not fixed.
I tried creating this with the hbox, vbox and gridlayout but they either put all the buttons below each other, or side by side. Also it resizes my mainwindow to an enormous size to fit all the buttons in.
Any Ideas?
Put QScrollArea inside your QMainWindow. Apply QGridLayout to QScrollArea's inside widget and put the buttons inside it.

QT - Place Buttons on Bottom Right

I am trying to place a set of buttons so that they are anchored to the bottom right of the screen. My problem is that whenever I resize the screen, the buttons are not anchored to the bottom right, but stay in its current position.
I have placed two Push Buttons inside a Horizontal Layout. I then placed this layout inside a Grid Layout, which contains a Horizontal and Vertical Spacer. I have modified the Grid Layout layoutSize property to SetMaximumSize.
What am I doing incorrectly, so that I can get my buttons to be anchored to the bottom right?
You have almost everything just right here, but you probably overlooked something that is really easy to miss when you first start using Qt Designer.
Your grid layout is sitting inside your widget with a fixed size and position. It too needs to be managed by a layout. If you take a look at the Object Inspector on the top right (that contains your hierarchy) you will probably see your top level widget with a red icon. This indicates that it contains no layout. You have two options to fix this...
Have your existing grid layout placed into another main layout (like a vertical layout). You would simply right click on your top level widget in the Object Inspector -> Lay Out -> [Choose a main layout type].
Have your grid be the main layout. To do this you would need to remove the grid layout and have your child items arranged exactly how you have them in that picture. Then follow the previous option, right clicking on the top level widget (or the blank background) and choose Lay out -> Grid. This will pop your widgets into a Grid at a best visual fit (which you can then fix if needed), and your grid will be the top level layout.
That grid layout will make placing other widgets quite hard. Try this instead:
Add (from left to right) horizontal spacer and the two buttons.
Multiselect them all.
Select "Lay Out Horizontally" (Ctrl-H) from the Qt Designer's (or Qt Creator's) top toolbar (not from the widget box in the left!).
Add vertical spacer on top of the previous widgets.
Select the main window by clicking it (none of the added widgets are now selected).
Select "Lay Out Vertically" (Ctrl-L) from the top toolbar.
Done.
It seems that you're doing it correctly. Just forgot to apply a layout to your central widget, right? The Grid layout should be arranged in your central widget. The more convenient way is to remove grid layout widget and lay out the central widget in a grid ;-)

Set widget in center in QDesigner

Is it possible to set widget in center of dialog or windows in QDesigner, say I have a widget contain 3 line edits and labels and I want to set this widget in center of window.
In latest Qt Designer, right-click a widget in the Object Inspector and choose your preference under Layout Alignment.
It should be quite simple:
Insert horizontal spacer
Set Horizontal layout
Insert Vertical layout
Insert another horizontal spacer
Add Vertical spacer to Horizontal spacer (drag to widget tree)
Add your widgets .... labels, textedits
Add another vertical sapcer to Horizontal space
Works in my QtCreator, should also in Your Designer :)
Good luck.
Add a layout to your windows and add the widgets in the layout. Then you can set the position policy in the properties panel of the layout.

Resources