PDA

View Full Version : Qt - C++ - ScrollArea - Resize content widgets by keeping the aspect ratio



Vildnex
16th June 2020, 20:05
I'm new to Qt and I've encountered the following problem.

I have a layout that looks like this.

Where:

Blue: rectangle it's a ScrollArea

Orange: rectangles are the widgets from that ScrollArea

13474


----------

The problem arises when I'm trying to resize the window. In the moment of resizing, I want my widgets to keep their aspect ratio. But that's not going to happen.

Cases:


If I will scale my application vertically my widgets are going to get way higher then they should. Instead, they should keep their current size because any vertical scaling will destroy they
the same goes also for horizontal scaling
at the same time if I will scale on horizontal and vertical at the same time I should get the same number of elements from that scroll area on the view. Doesn't matter the size of them




----------

Now my question arises... this resizing problem is caused by the scroll area or by the widget itself? How can I fix this?


----------


My code:




ui->setupUi(this);

ui->scrollArea->setWidgetResizable(true);

for (auto i = 0; i < 10; ++i) {
auto *targetWidget = new PolygonButtonWidget();//let it be target object
ui->scrollAreaWidgetContents->layout()->addWidget(targetWidget);
}
QScroller::grabGesture(ui->scrollArea, QScroller::LeftMouseButtonGesture);

Does anyone know how can I achieve my wanted result?

Ginsengelf
17th June 2020, 09:47
Hi, you can try QWidget::hasHeightForWidth() and QWidget::heightForWidth() to "return preferred height for this widget, given the width w."

Ginsengelf

Vildnex
17th June 2020, 22:18
is not actually working because that ORANGE widget which needs to be resized needs to be inside of a layout. Well in my case is not, so hasHeightForWidth is going to return all the times false