Square custom QWidgets inside QGridLayout inside QScrollArea



  • Hello,
    I'm trying to lay out a grid of square custom (subclassed) QWidgets inside a QGridLayout and QScrollArea.

    The way I want it to work is choosing the number of QGridLayout columns and creating squares of the correct sizes.

    What I've tried doing already is

    • Manually laying out/resizing the QWidgets but I feel this is a bit sloppy and I couldn't get it quite right
    • Setting QScrollArea::widgetsResized to true which does resize the width correctly, but not the height, see screenshot.

    I've tried setting QSizePolicy and overriding QWidget::heightForWidth along with setting QScrollArea::widgetsResized in my custom QWidget-derived class, like so:

    CustomWidget::CustomWidget(...) 
    {  
      ...
    
      QSizePolicy policy(QSizePolicy::Preferred, QSizePolicy::Preferred);
      policy.setHeightForWidth(true);
    
      setSizePolicy(policy);
    }
    
    ...
    
    int CustomWidget::heightForWidth(int width) const
    {
        return width; // square
    }
    

    But CustomWidget::heightForWidth is never called.

    Any help would be appreciated.




Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.