Qt - 从布局中删除所有小部件?

新手上路,请多包涵

这似乎并不容易。基本上,我通过一个函数将 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 许可协议

阅读 784
1 个回答

PyQt5 中存在某种错误,如果您使用上述方法,布局中仍会显示未删除的项目。所以我只是删除布局并重新开始:

例如

   def run_selected_procedures(self):
      self.commandListLayout.deleteLater()
      self.commandListLayout = QVBoxLayout()
      widget = QWidget()
      widget.setLayout(self.commandListLayout)
      self.scrollArea.setWidget(widget)
      self.run_procedures.emit(self._procSelection)

原文由 Daniel Donnelly 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题