Search code examples
c++qtqscrollareaqgridlayout

Add QScrollArea to ui->centralWidget with QGridLayout


I want to make the ui->centralWidget, which has a QGridLayout, scrollable. What is the easiest and most comfortable way to do this?

I tried with a minimalistic project, but it does not work correctly. The QScrollArea appears in a second window and the test label is not shown in none of the shown windows. What am I doing wrong?

mainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    gridLayout = new QGridLayout();
    ui->centralWidget->setLayout(gridLayout);

    scrollArea = new QScrollArea();
    scrollArea->setBackgroundRole(QPalette::Dark);
    scrollArea->setWidget(ui->centralWidget);

    test = new QLabel("Test");
    gridLayout->addWidget(test,0,0);
    scrollArea->show();
}

MainWindow::~MainWindow()
{
    delete ui;
}

mainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGridLayout>
#include <QScrollArea>
#include <QLabel>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QGridLayout *gridLayout;
    QScrollArea *scrollArea;
    QLabel *test;
};

#endif // MAINWINDOW_H

Changes after hint of @thuga:

The label is not displayed.

mainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QWidget *testWidget = new QWidget(ui->centralWidget);
    gridLayout = new QGridLayout(testWidget);
    testWidget->setLayout(gridLayout);

    scrollArea = new QScrollArea;
    //scrollArea->setBackgroundRole(QPalette::Dark);
    scrollArea->setWidget(testWidget);
    scrollArea->setFrameShape(QFrame::NoFrame);

    QHBoxLayout *mainLayout = new QHBoxLayout(ui->centralWidget);
    ui->centralWidget->setLayout(mainLayout);
    mainLayout->addWidget(scrollArea);

    test = new QLabel("TESTTESTTESTTESTTEST");
    testWidget->layout()->addWidget(test);
}

MainWindow::~MainWindow()
{
    delete ui;
}

Solution

  • @thuga helped to find a solution, thanks. I want to share it. Maybe it will be helpful for someone.

    Here is the code for a minimalistic application:

    mainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        mainWidget = new QWidget(ui->centralWidget);
        gridLayout = new QGridLayout(mainWidget);
        mainWidget->setLayout(gridLayout);
    
        scrollArea = new QScrollArea;
        scrollArea->setWidget(mainWidget);
        scrollArea->setWidgetResizable(true);
    
        mainLayout = new QHBoxLayout(ui->centralWidget);
        ui->centralWidget->setLayout(mainLayout);
        mainLayout->addWidget(scrollArea);
    
        testLabel = new QLabel("TESTTESTTESTTEST");
        gridLayout->addWidget(testLabel,0,0);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    mainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QGridLayout>
    #include <QScrollArea>
    #include <QLabel>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
        QWidget *mainWidget;
        QHBoxLayout *mainLayout;
        QGridLayout *gridLayout;
        QScrollArea *scrollArea;
        QLabel *testLabel;
    };
    
    #endif // MAINWINDOW_H