Search code examples
qtqscrollarea

How to create a scrollable widget using QHBoxLayout and QScrollArea


I am trying to create a widget with scrollable using the QHBoxLayout and QScrollArea but i am unable to succeed to create a GUI,here is my code can any one please suggest me where i need to change. Any help would be appreciated

mainLayout = new QHBoxLayout(this);
mainLayout->setSizeConstraint(QLayout::SetMaximumSize);
QCheckBox *sam_box = new QCheckBox(this);
sam_box->setText("HAIIIIIIIIIII");
sam_box->setFixedSize(10000,10000);
QCheckBox *so_box = new QCheckBox(this);
so_box->setText("HOWWWWWWWWWWWW");
so_box->setFixedSize(150000,15000);
mainLayout->addWidget(sam_box);
mainLayout->addWidget(so_box);

QScrollArea *scrollarea = new QScrollArea(this);
scrollarea->setBackgroundRole(QPalette::Shadow);
scrollarea->setFixedSize(700,500);
scrollarea->setLayout(mainLayout);`

Here is my output screen

Thanks in advance, Rohith.G


Solution

  • One way to achieve this is by below logic centralwidget() -> QHBoxLayout-> QScrollArea -> QWidget-> add the check box

    The code flow & comments will explain the logic in detail.

    QWidget *wgt = new QWidget();//new code
        wgt->setGeometry(0,0,500,500); //new code
        //QHBoxLayout mainLayout = new QHBoxLayout(this);
        QHBoxLayout *mainLayout = new QHBoxLayout(this->centralWidget());
    
        mainLayout->setSizeConstraint(QLayout::SetMaximumSize);
        //QCheckBox *sam_box = new QCheckBox(this);
        QCheckBox *sam_box = new QCheckBox(wgt);
        sam_box->setText("HAIIIIIIIIIII");
    
        //sam_box->setFixedSize(10000,10000);
        sam_box->setFixedSize(200,200); //make it small for quick testing
    
        //QCheckBox *so_box = new QCheckBox(this);
        QCheckBox *so_box = new QCheckBox(wgt);
        so_box->setText("HOWWWWWWWWWWWW");
    
        //so_box->setFixedSize(150000,15000);
        so_box->setFixedSize(150,150); //make it small for quick testing
    
        //mainLayout->addWidget(sam_box);
        //mainLayout->addWidget(so_box);
    
        QScrollArea *scrollarea = new QScrollArea();
        scrollarea->setBackgroundRole(QPalette::Shadow);
        //scrollarea->setFixedSize(700,500); //scroll area cant be resized
        mainLayout->addWidget(scrollarea); //new code
        //scrollarea->setLayout(mainLayout);
        scrollarea->setWidget(wgt);
    

    enter image description here

    Below code will generate 30 QCheckBoxes added each ten in a vertical box layout and all the vertical layout in a horizontal box layout

    QScrollArea *scrl = new QScrollArea();
        scrl->setGeometry(0,0,300,300);
        QWidget *wgtMain = new QWidget();
        QHBoxLayout *hboxMain = new QHBoxLayout(wgtMain);
        for(int iCount=0;iCount<3;iCount++){
            QWidget *wgtSub = new QWidget();
            QVBoxLayout *vboxSub = new QVBoxLayout(wgtSub);
            for(int jCount=0;jCount<10;jCount++){
                QCheckBox *chk = new QCheckBox("Check Box " + QString::number(jCount+1) + " of " + QString::number(iCount+1));
                vboxSub->addWidget(chk);
            }
            hboxMain->addWidget(wgtSub);
        }
        scrl->setWidget(wgtMain);
        scrl->show();
    

    enter image description here