为什么是布局,不更新?

Why is the layout, don't update?

我需要从布局中清除我的 chartViewer,但是当我这样做时,我无法删除最后一个小部件。 当我测试我的布局的长度时,我得到 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, 0,当我们将它添加到布局时,这个位置由它处理,所以如果我们从布局中删除它,它将 return到它的初始位置,视觉上产生不被删除的效果,那么解决它我们必须删除它的widget我们使用方法deleteLater()

有解决方案,我的小部件仍然附加到布局,即使在 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)