Results 1 to 4 of 4

Thread: How to update size of nested QWidget?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2020
    Posts
    19
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default How to update size of nested QWidget?

    I'm using Qt 5.15 with C++17. I have a hierarchy of Widgets like hinted in the title:
    Qt Code:
    1. MainWindow : QMainWindow
    2. ->centralWidget() = QScrollArea
    3. ->widget() = QWidget
    4. ->layout() = QVBoxLayout
    5. ->children() = [
    6. InnerWidget,
    7. InnerWidget,
    8. InnerWidget,
    9. ...
    10. ]
    To copy to clipboard, switch view to plain text mode 
    with
    Qt Code:
    1. InnerWidget.children() = [ QLabel, QLabel ]
    To copy to clipboard, switch view to plain text mode 
    .
    I have set
    Qt Code:
    1. InnerWidget::setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum)
    To copy to clipboard, switch view to plain text mode 
    inside InnerWidget's constructor.

    The QLabels inside InnerWidget are initialized with some QPixmap like
    Qt Code:
    1. label->setPixmap(img);
    2. label->setFixedSize(img.size());
    To copy to clipboard, switch view to plain text mode 
    _________________


    This all works fine, however now I added an option to zoom all the pixmaps to the MainWindow's menubar. I connect to a signal MainWindow::zoom_inner(.., which itself calls InnerWidget::zoom(.. on every InnerWidget, which resizes the images / labels like this:
    Qt Code:
    1. auto scaled = img.scaled(img.size() * factor);
    2. label->setPixmap(scaled);
    3. label->setFixedSize(scaled.size());
    To copy to clipboard, switch view to plain text mode 

    Finally I update the size of the MainWindow by finding the maximum InnerWidget.frameGeometry().width() (= inner_max_width) and calling
    Qt Code:
    1. void MainWindow::zoom_inner() {
    2. // ...
    3. setMaximumWidth(inner_max_width + centralWidget()->verticalScrollBar()->width() + 1);
    4. resize(maximumWidth(), size().height());
    5. }
    To copy to clipboard, switch view to plain text mode 
    _________________

    Now after calling MainWindow::zoom(.., the window seems to resize to the correct size, as do the QLabel's inside the InnerWidget's. However, the InnerWidget's themselves do not resize at all, they just stay the same size as before and I have no idea why. I tried calling many combinations of
    Qt Code:
    1. adjustSize();
    2. update();
    3. layout()->update();
    4. updateGeometry();
    To copy to clipboard, switch view to plain text mode 
    on any of the MainWindow,QScrollArea,InnerWidget but nothing happens.. even calling
    Qt Code:
    1. InnerWidget::resize(new_size)
    To copy to clipboard, switch view to plain text mode 
    has no effect. Clearly InnerWidget::sizeHint() gives the correct value, because the MainWindow size after MainWindow::zoom(.. is correct. So why do the InnerWidget's refuse to resize to fit the QLabel's, even though sizePolicy is set to QSizePolicy::Minimum?

    This seems like some sort of misunderstanding on my part so I'm hoping the answer is simple and doesn't need a MWE. I've tried the docs but couldn't find a solution.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to update size of nested QWidget?

    sizePolicy is set to QSizePolicy::Minimum
    This is telling the VBox layout that the widget's sizes are fixed at their minimum sizes. You need a size policy that includes "Expanding" or use "Preferred".
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QWidget::size() vs QWidget::geometry()
    By 33333 in forum Newbie
    Replies: 1
    Last Post: 13th June 2014, 07:06
  2. Replies: 1
    Last Post: 30th May 2014, 17:09
  3. QWidget update get so cpu usage
    By danics in forum Qt Programming
    Replies: 8
    Last Post: 20th August 2012, 08:07
  4. QWidget::update does not work!
    By sapali in forum Qt Programming
    Replies: 8
    Last Post: 17th March 2011, 16:56
  5. update() in QWidget
    By salmanmanekia in forum Qt Programming
    Replies: 5
    Last Post: 28th July 2008, 08:03

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.