QT问题中的滚动区域?

QT问题中的滚动区域?,qt,Qt,我有一个表单,我正在应用ScrollArea,但它没有得到正确应用? 我想把这四个按钮放在可滚动的区域,当按钮增加时,滚动条应该出现在那里,垂直向下滚动 当前,当我尝试添加更多按钮时,按钮与组合框重叠,并且不会向下增长 这是我的密码: QPushButton *b1 = new QPushButton(strsearch); QPushButton *b2 = new QPushButton(strsearch); QPushButton *b3 =

我有一个表单,我正在应用ScrollArea,但它没有得到正确应用? 我想把这四个按钮放在可滚动的区域,当按钮增加时,滚动条应该出现在那里,垂直向下滚动

当前,当我尝试添加更多按钮时,按钮与组合框重叠,并且不会向下增长

这是我的密码:

 QPushButton *b1 = new QPushButton(strsearch);
           QPushButton *b2 = new QPushButton(strsearch);
           QPushButton *b3 = new QPushButton(strsearch);
           QPushButton *b4 = new QPushButton(strsearch);

 b1->setStyleSheet(

               "background-image: url(:/user.gif);"
               "background-repeat:no-repeat;"
               "background-attachment:fixed;"
               "background-position:left top;"
               "border-style: outset;"
               "background-color : black;"
               "border-width: 2px;"
               "border-radius: 10px;"
               "border-color: black;"
               "font: bold 16px;"
               "color: black;"
               "min-width: 10em;"
               "min-height: 0.75em;"
               "margin: 0 1px 0 1px;"
               "color:rgb(255,255,255);"
               "padding: 6px;"
               );



b2->setIcon(QIcon(":/user.gif"));
           b2->setIconSize(QSize(160, 26));
           b3->setIcon(QIcon(":/user.gif"));
           b3->setIconSize(QSize(160, 26));
           b4->setIcon(QIcon(":/user.gif"));
           b4->setIconSize(QSize(160, 26));

 QGridLayout *layout = new QGridLayout;
 layout->addWidget(b1, 1, 0);
           layout->addWidget(b2, 2, 0);
            layout->addWidget(b3, 3, 0);
            layout->addWidget(b4, 4, 0);
layout->addWidget(scrollArea);
  layout->setAlignment(Qt::AlignBottom);
  setLayout(layout);

@里卡多:但我只想将滚动应用到按钮区域,它们根据您的代码放置在QVBoxLayout中。应用mainLayout->addWidgetscrollArea;需要什么;。我可以这样做吗?scrollLayout->addWidgetscrollArea@Riccardo:如果控件被添加到主布局中,如何检查此项?不要再添加它?在布局中,就像在其他每个QObject中一样,您可以使用子对象来获取所有子对象的列表,然后使用QList的contains方法搜索您的对象。QObject list=layout->children;iflist.containsmyobject{//code}@Riccardo:但是QObjectList list=scrollLayout->children每次都包含0个项目???@Riccardo:但是我只想将滚动应用到按钮区域,它们根据您的代码放置在QVBoxLayout中。需要应用mainLayout->addWidgetscrollArea;。我可以这样做吗?scrollLayout->addWidgetscrollArea@Riccardo:如果控件被添加到主布局中,如何检查此项?不要再添加它?在布局中,就像在其他每个QObject中一样,您可以使用子对象来获取所有子对象的列表,然后使用QList的contains方法搜索您的对象。QObject list=layout->children;iflist.containsmyobject{//code}@Riccardo:但是QObjectList list=scrollLayout->子对象每次包含0个项目??
//Create the buttons
QPushButton *b1 = new QPushButton("Button 1");
QPushButton *b2 = new QPushButton("Button 2");
QPushButton *b3 = new QPushButton("Button 3");
QPushButton *b4 = new QPushButton("Button 4");

//Add the buttons to a vertical layout (faster than grid layout)
QVBoxLayout *scrollLayout = new QVBoxLayout;
scrollLayout->addWidget(b1);
scrollLayout->addWidget(b2);
scrollLayout->addWidget(b3);
scrollLayout->addWidget(b4);

//Create a viewport widget that contains the layout with buttons
QWidget *viewport = new QWidget;
viewport->setLayout(scrollLayout);

//Add the viewport to the scroll area
QScrollArea *scrollArea = new QScrollArea;
scrollArea->setWidget(viewport);

//Add the scroll area to your main window's layout
mainLayout->addWidget(scrollArea);