我需要从布局中清除我的chartViewers,但是当我这样做时,我无法删除最后一个小部件。 当我测试布局的长度时,我得到0但是在更新布局后窗口小部件仍在这里,如图所示:
这是我删除时的代码,小部件和图表
print("proceding to delete chart ", chartName, " at : ", indexGraph)
currentGraph = self.charts[indexGraph]
currentWidget = self.chartVs[indexGraph]
self.chartLayout.removeWidget(currentWidget)
self.chartVs.remove(currentWidget)
currentGraph.clearData()
self.charts.remove(currentGraph)
self.chartLayout.update()
#currentWidget.resetCachedContent()
listGraphs.remove(chartName)
self.refreshListWithOpt(self.chartTree, listGraphs, 1, optGraphs)
以下是我创建图表并将其添加到布局的代码:
self.charts.append(chartClass(patientStr, exp))
print("\nNew Plot chart ", self.charts[lastIndex].name, " length : ", lastIndex )
listGraphs.append(self.charts[lastIndex].name)
print("list Graphs : ", listGraphs)
self.charts[lastIndex].plotJSON(myData.plot(patientStr, exp))
self.chartVs.append(QChartView(self.charts[lastIndex]))
self.chartVs[lastIndex].setRenderHint(QPainter.Antialiasing)
self.chartLayout.insertWidget(0, self.chartVs[lastIndex])
有什么建议吗?
答案 0 :(得分:1)
当一个小部件被添加到主小部件时,默认位置是0,0,当我们将它添加到布局时,这个位置由它处理,所以如果我们将它从布局中移除它将返回到它的初始位置,在视觉上产生不被删除的效果,然后为了解决它我们必须删除它的小部件我们使用方法deleteLater()
答案 1 :(得分:1)
有解决方案,我的小部件仍然附加到布局,即使在removeItem之后。所以解决方案是widgetToDelete.deleteLater()
或widgetToDelete.setParent(None)
print("proceding to delete chart ", chartName, " at : ", indexGraph)
currentGraph = self.charts[indexGraph]
currentWidget = self.chartVs[indexGraph]
self.chartLayout.removeItem(currentWidgetItem)
currentWidgetItem.widget().deleteLater()
#currentWidgetItem.widget().setParent(None)
self.chartLayout.removeItem(currentWidgetItem)
self.chartVs.remove(currentWidget)
self.chartLayout.update()
#currentWidget.resetCachedContent()
listGraphs.remove(chartName)
self.refreshListWithOpt(self.chartTree, listGraphs, 1, optGraphs)