0

I am trying to put a grid view that will contain some buttons into a scroll area but I don't know why the grid layout only fits into the initial size of the scroll area (it doesn't go downside so I could use the scroll bar to see all elements).

ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
QGridLayout* lay=new QGridLayout(this);
QPushButton *name[100];
for(int i=0;i<10;i++){

    QString str1="project"+QString::number(i);
    QString str2="3/6task";
    QString str3="ALEX";

    name[i]=new QPushButton(str1);

 
    name[i]->setObjectName("btn_1");
    name[i]->setStyleSheet("QPushButton#btn_1{background:transparent;Text-align:left;font-family:century gothic;font-size:18px;color:red;}"
                            "QPushButton#btn_1:hover{color:yellow;Font-size:22px;}");

    name[i]->setFixedSize(100,40);
    lay->addWidget(name[i]);
 
}
ui->scrollArea->setLayout(lay);

The result is in the next photo:

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

3 Answers3

0

You have to use the following code:

This code has been tested.

QScrollArea* scroll = new QScrollArea(this);
scroll->setGeometry(50,0,250,300);
scroll->setWidgetResizable(true);

QWidget *container = new QWidget;
scroll->setWidget(container);
QGridLayout* lay = new QGridLayout(container);

QPushButton *name[100];
for(int i = 0; i < 20; ++i) {
    name[i]=new QPushButton(scroll);
    lay->addWidget(name[i]);
}

Of cource you can modify the button and the scrollArea as well.

There is no problem of using ui->scrollArea

0

Your applying the QGridLayout to the QScollArea when you actually need to apply it to a QWidget managed by the scroll area using QScrollArea::setWidget...

#include <QApplication>
#include <QGridLayout>
#include <QPushButton>
#include <QScrollArea>
#include <QString>
#include <QWidget>

int main (int argc, char **argv)
{
    QApplication app(argc, argv);
    QScrollArea scroll_area;
    scroll_area.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    QWidget viewport;
    QGridLayout layout(&viewport);
    for (int i = 0; i < 10; ++i) {
        QString str1 = "project" + QString::number(i);
        QString str2 = "3/6task";
        QString str3 = "ALEX";

        auto *pb = new QPushButton(str1);

        pb->setObjectName("btn_1");
        pb->setStyleSheet("QPushButton#btn_1{background:transparent;Text-align:left;font-family:century gothic;font-size:18px;color:red;}"
                          "QPushButton#btn_1:hover{color:yellow;Font-size:22px;}");

        pb->setFixedSize(100,40);
        layout.addWidget(pb);
    }
    scroll_area.setWidget(&viewport); 
    scroll_area.show();
    return app.exec();
}
G.M.
  • 12,232
  • 2
  • 15
  • 18
0

I fixed it by this way:

   ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    QGridLayout *lay = new QGridLayout(ui->centralwidget);
    lay->addWidget(ui->scrollArea, 0, 0, 1, 1);

    auto  gridLayout = new QGridLayout(ui->scrollAreaWidgetContents);
    gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    auto  widget = new QWidget(ui->scrollAreaWidgetContents);
    widget->setObjectName(QString::fromUtf8("widget"));
    auto  gridLayout_2 = new QGridLayout(widget);
    gridLayout_2->setObjectName(QString::fromUtf8("gridLayout_2"));

    gridLayout->addWidget(widget, 0, 0, 1, 1);

    for (int i = 0; i < 10; i++)
    {
        QString      str1 = "project" + QString::number(i);
        QString      str2 = "3/6task";
        QString      str3 = "ALEX";
        QPushButton *btn  = new QPushButton(str1);


        btn->setObjectName(QString("btn_%1").arg(i));
        btn->setStyleSheet("QPushButton#btn_1{background:transparent;Text-align:left;font-family:century gothic;font-size:18px;color:red;}"
                           "QPushButton#btn_1:hover{color:yellow;Font-size:22px;}");

        btn->setFixedSize(100, 40);
        widget->layout()->addWidget(btn);
    }
}

enter image description here

Parisa.H.R
  • 3,303
  • 3
  • 19
  • 38
  • Thanks a lot! But can you explain a little what you did there, please? I dont understand exactly what i did wrong –  Aug 16 '21 at 21:23
  • 1
    I create a widget and put it in scroll area and create grid layout for buttons and set widget as parent , and when you use for loop you didn't need array each time you create a button and new it and finally add it in widget's layout. – Parisa.H.R Aug 17 '21 at 04:10
  • 1
    In scroll area you can't add your buttons directly , but it has setwidget function , problem is it set only one widget. So I create a widget and add buttons there. – Parisa.H.R Aug 17 '21 at 04:17
  • but grid_layout2 is not used, right? –  Aug 17 '21 at 10:09
  • 1
    @BadeaV one grid layout for scroll area that contains widget , one grid layout inside widget that contains buttons, grid layout 2 is for buttons – Parisa.H.R Aug 17 '21 at 10:18