Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Custom widget got squashed in scroll area



  • I am trying to add custom widgets into a scroll area expecting the scroll bar will appear when widgets are getting many while the widgets remain its original size.

    this is my custom widget:
    0_1556468964511_1.PNG

    and this is the main window:
    0_1556469047487_2.PNG
    1_1556468987670_3.PNG

    The button in the main window adds a custom widget each click.
    When I try to click the button once:
    0_1556469143898_4.PNG

    Now I click the button for three times:
    0_1556469197431_5.PNG

    But it works when I add QPushButtons:
    0_1556469292215_6.PNG

    this is the code of mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <modal.h>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        ui->scrollAreaWidgetContents->setLayout(ui->verticalLayout_3);
    
        ui->scrollArea->setWidgetResizable(true);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
    
        // add custom widget
        ui->scrollArea->widget()->layout()->addWidget(new modal(this));
    
        ui->scrollArea->ensureVisible(1000,1000);
    
        ui->scrollArea->ensureWidgetVisible(ui->scrollArea->widget()->layout()->widget());
    }
    
    

    I am new to QT, this got me puzzled for a weeks. Thanks in advance for the help


  • Lifetime Qt Champion

    Hi and welcome to the forums
    Good post +1
    Its due to setMinimumSize that you can set on all Widgets.
    The CalenderWidget/customWidget has none set which means scroll areas layout is allowed to make them smaller and smaller
    when more is added.
    QPushbutton, on the other hand, has FixedSize for its height so they are not compressed. (made smaller)

    To fix it, you can simply set a minimum size/height for the calendar widget/its form

    modal * NewModal = new modal(this)
    NewModal ->setMinimumHeight(200); // wont become smaller than this then
     ui->scrollArea->widget()->layout()->addWidget(NewModal );
    

    QPushButton
    alt text

    compare that to the CalenderWidget/CustomWidget/the "modal" form



  • It wooooooorrrrkkks !!! Thank you so much 💋


Log in to reply