Orphan QScrollArea remains after adding to layout - qt

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?

Related

Scrolling QTableWidget smoothly

I have a QTableWidget with custom Widgets. These widgets are big and fill almost the whole scroll area in height, so that only one row is visible.
I can scroll with the mouse wheel or by dragging the scroll bar, but the row always jumps.
Is there a way to configure the QTableWidget to smoothly scroll, without jumping?
Try to use this:
view->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel)
From Qt documentation:
enum ScrollMode { ScrollPerItem, ScrollPerPixel }
verticalScrollMode : ScrollMode
This property holds how the view scrolls its contents in the vertical
direction.
This property controls how the view scroll its contents vertically.
Scrolling can be done either per pixel or per item.

Qt: How to fit content to QScrollArea

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)

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

QScollArea doesn't autoscroll when dragging inside it

I have some widgets inside a QScrollArea and I'm dragging between these widgets, the problem is that the QScrollArea doesn't scroll when I'm dragging inside it, so if I want to drag between a widget A to the widget B and the widget B is not visible on the viewport, the QScrollArea doesn't automatically scroll when the mouse moves to the edge of the viewport.
Subclass your scroll area, and add in one of the functions below.
http://qt-project.org/doc/qt-4.8/qwidget.html#mousePressEvent
http://qt-project.org/doc/qt-4.8/qwidget.html#dragMoveEvent
http://qt-project.org/doc/qt-4.8/qwidget.html#dragLeaveEvent // Probably just need this one
When the dragMoveEvent reaches the edge of your widget, or when the dragLeaveEvent happens, detect which edge it is, or left at, and then scroll your area in that direction.
Hope that helps.

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

Resources