我正在尝试使用QHBoxLayout和QScrollArea创建一个可滚动的小部件,但我无法成功创建GUI,这里是我的代码可以任何一个请建议我需要更改的地方。 任何帮助将不胜感激
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);`
提前致谢, Rohith.G
答案 0 :(得分:2)
实现这一目标的一种方法是通过以下逻辑 centralwidget() - > QHBoxLayout-> QScrollArea - > QWidget->添加复选框
代码流程&评论将详细解释逻辑。
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);
下面的代码将生成30个QCheckBoxes,每个垂直框布局中添加10个,所有垂直布局都放在水平框布局中
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();