Results 1 to 3 of 3

Thread: Qt - C++ - ScrollArea - Resize content widgets by keeping the aspect ratio

  1. #1
    Join Date
    Mar 2020
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Qt - C++ - ScrollArea - Resize content widgets by keeping the aspect ratio

    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

    345ZZ.png


    ----------

    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:


    Qt Code:
    1. ui->setupUi(this);
    2.  
    3. ui->scrollArea->setWidgetResizable(true);
    4.  
    5. for (auto i = 0; i < 10; ++i) {
    6. auto *targetWidget = new PolygonButtonWidget();//let it be target object
    7. ui->scrollAreaWidgetContents->layout()->addWidget(targetWidget);
    8. }
    9. QScroller::grabGesture(ui->scrollArea, QScroller::LeftMouseButtonGesture);
    To copy to clipboard, switch view to plain text mode 
    Does anyone know how can I achieve my wanted result?
    Attached Images Attached Images

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    454
    Thanks
    10
    Thanked 60 Times in 58 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt - C++ - ScrollArea - Resize content widgets by keeping the aspect ratio

    Hi, you can try QWidget::hasHeightForWidth() and QWidget::heightForWidth() to "return preferred height for this widget, given the width w."

    Ginsengelf

  3. #3
    Join Date
    Mar 2020
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt - C++ - ScrollArea - Resize content widgets by keeping the aspect ratio

    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

Similar Threads

  1. Replies: 3
    Last Post: 2nd September 2016, 13:44
  2. Replies: 3
    Last Post: 27th May 2015, 00:14
  3. Replies: 2
    Last Post: 7th December 2013, 20:02
  4. Replies: 0
    Last Post: 21st November 2011, 08:55
  5. keeping aspect ratio while resizing
    By franco.amato in forum Qt Programming
    Replies: 2
    Last Post: 19th November 2009, 21:12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.