这似乎并不容易。基本上,我通过一个函数将 QPushButton
s 添加到布局中,当函数执行时,我想先清除布局(删除所有 QPushButton
s 以及那里的任何其他内容) ,因为更多按钮只是附加到 scrollview
。
标题
QVBoxLayout* _layout;
cpp
void MainWindow::removeButtonsThenAddMore(const QString &item) {
//remove buttons/widgets
QVBoxLayout* _layout = new QVBoxLayout(this);
QPushButton button = new QPushButton(item);
_layout->addWidget(button);
QPushButton button = new QPushButton("button");
_layout->addWidget(button);
QWidget* widget = new QWidget();
widget->setLayout(_layout);
QScrollArea* scroll = new QScrollArea();
scroll->setWidget(widget);
scroll->show();
}
原文由 user375566 发布,翻译遵循 CC BY-SA 4.0 许可协议
PyQt5 中存在某种错误,如果您使用上述方法,布局中仍会显示未删除的项目。所以我只是删除布局并重新开始:
例如