0

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

rohith
  • 35
  • 1
  • 8

1 Answers1

3

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

Jeet
  • 1,006
  • 1
  • 14
  • 25
  • Hi thanks for replying,can you please say me what is centralWidget i.e is it the main Widget..? – rohith Feb 06 '16 at 09:06
  • Hi, Jeet please ignore my previous comment,i am using the normal Widget and i am not using the MainWindow will there be any change if i use the Widget rather than the MainWindow – rohith Feb 06 '16 at 09:29
  • @rohith, You can use widget rather than the main window. if you are not going to use the qmainwindow (using wizard), then use widget* instead of the centralwidget(). – Jeet Feb 06 '16 at 09:41
  • Hi,Jeet your solution is very helpful for me and a small doubt i need to add a set of checkboxes(1-10) to verticallayout_1 and checkboxes(11-20) to and i have to verticalLayout_2 and (21-30) to verticallayout_3 and so on all these verticalLayouts need to be added to a HorizontalLayout will this logic work:- widget* -> QHBoxLayout-> QVBoxLayout->QScrollArea -> QWidget-> add the check box. – rohith Feb 06 '16 at 10:15
  • Thanks a lot JEET thanks for your help and support..:) – rohith Feb 06 '16 at 10:57