1

我需要从布局中清除我的 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])

有什么建议吗?

4

2 回答 2

1

当一个小部件添加到主小部件时,默认位置是 0, 0,当我们将它添加到布局时,这个位置由它处理,所以如果我们从布局中删除它,它将返回到它的初始位置,直观地生成没有被删除的效果,那么要解决它我们必须删除它的小部件我们使用方法deleteLater()

于 2017-09-12T16:14:09.870 回答
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)
于 2017-09-12T16:17:42.723 回答