Solved 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:
and this is the main window:
The button in the main window adds a custom widget each click.
When I try to click the button once:
Now I click the button for three times:
But it works when I add QPushButtons:
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
-
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
compare that to the CalenderWidget/CustomWidget/the "modal" form
-
It wooooooorrrrkkks !!! Thank you so much 💋