Skip to content

Instantly share code, notes, and snippets.

@stylesuxx
Last active July 1, 2019 23:13
Show Gist options
  • Save stylesuxx/10304993 to your computer and use it in GitHub Desktop.
Save stylesuxx/10304993 to your computer and use it in GitHub Desktop.
Expand the widgets inside a scrollArea to the maximum possible size, or the window to the minimum possible size.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QScrollArea>
#include <QSlider>
#include <QWidget>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QVBoxLayout *mainLayout = new QVBoxLayout();
// Add sliders to the layout so it overflows the window height for sure
for(int i = 0; i < 100; i++) {
QSlider *slider = new QSlider(Qt::Horizontal);
mainLayout->addWidget(slider);
}
QWidget *mainWidget = new QWidget();
mainWidget->setLayout(mainLayout);
QScrollArea *scrollArea = new QScrollArea();
scrollArea->setWidgetResizable(true);
scrollArea->setWidget(mainWidget);
setCentralWidget(scrollArea);
}
MainWindow::~MainWindow()
{
delete ui;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment