Мой предыдущий вопрос Вставьте QChartView в пользовательский интерфейс
def draw_chart(self, obj, chart_view, replace=0):
obj.setContentsMargins(0, 0, 0, 0)
lay = QtWidgets.QHBoxLayout(obj)
lay.setContentsMargins(0, 0, 0, 0)
if replace:
# lay.replaceWidget(chart_view)
pass
else:
lay.addWidget(chart_view)
Я вызываю эту функцию следующим образом:
cv1 = self.candle_chart(self.p)
self.draw_chart(self.chart1, cv1, 0)
candle_chart
возвращает QtChart.QChartView
object, а self.chart1
- это имя объекта Qwidget
.
. Впервые работает нормально, но когда я хочу обновить диаграмму, выдается ошибка:
QLayout: Attempting to add QLayout "" to QWidget "chart1", which already has a layout
Я пытаюсь изменить данные диаграммы нажатием кнопки
завершить код:
class Test(base_3, form_3):
def __init__(self):
super(base_3, self).__init__()
self.setupUi(self)
self.p = [(1, 7380, 7520, 7380, 7510, 7324),
(2, 7520, 7580, 7410, 7440, 7372),
(3, 7440, 7650, 7310, 7520, 7434),
(4, 7450, 7640, 7450, 7550, 7480),
(5, 7510, 7590, 7460, 7490, 7502),
(6, 7500, 7590, 7480, 7560, 7512),
(7, 7560, 7830, 7540, 7800, 7584)]
self.next.clicked.connect(self.more)
self.lmt = 3
self.st = 0
cv1 = self.candle_chart(self.p[self.st:self.lmt])
self.draw_chart(self.chart1, cv1, 0)
def more(self):
cv1 = self.candle_chart(self.p[self.st:self.lmt])
self.draw_chart(self.chart1, cv1, 1)
self.st += 1
self.lmt += 1
def draw_chart(self, label, chart_view, replace=0):
label.setContentsMargins(0, 0, 0, 0)
lay = QtWidgets.QHBoxLayout(label)
lay.setContentsMargins(0, 0, 0, 0)
if replace:
# lay.replaceWidget(chart_view)
pass
else:
lay.addWidget(chart_view)
def candle_chart(self, data):
series = QtChart.QCandlestickSeries()
series.setDecreasingColor(QtCore.Qt.red)
series.setIncreasingColor(QtCore.Qt.green)
tm = []
i = 0
for d, o, h, l, c, v in data:
# x = x[1]
# series.append(QtChart.QCandlestickSet(x["open"], x["high"], x["low"], x["close"]))
series.append(QtChart.QCandlestickSet(o, h, l, c))
tm.append(str(i))
i += 1
chart = QtChart.QChart()
chart.addSeries(series)
chart.createDefaultAxes()
chart.legend().hide()
chart.axisX(series).setCategories(tm)
chart_view = QtChart.QChartView(chart)
return chart_view
код интерфейса:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1381</width>
<height>680</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="next">
<property name="geometry">
<rect>
<x>490</x>
<y>540</y>
<width>88</width>
<height>33</height>
</rect>
</property>
<property name="text">
<string>Next</string>
</property>
</widget>
<widget class="QWidget" name="chart1" native="true">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>460</width>
<height>250</height>
</rect>
</property>
</widget>
<widget class="QWidget" name="chart2" native="true">
<property name="geometry">
<rect>
<x>460</x>
<y>0</y>
<width>460</width>
<height>250</height>
</rect>
</property>
</widget>
<widget class="QWidget" name="chart3" native="true">
<property name="geometry">
<rect>
<x>920</x>
<y>0</y>
<width>460</width>
<height>250</height>
</rect>
</property>
</widget>
<widget class="QWidget" name="chart5" native="true">
<property name="geometry">
<rect>
<x>450</x>
<y>250</y>
<width>450</width>
<height>250</height>
</rect>
</property>
</widget>
<widget class="QWidget" name="chart6" native="true">
<property name="geometry">
<rect>
<x>900</x>
<y>250</y>
<width>450</width>
<height>250</height>
</rect>
</property>
</widget>
<widget class="QWidget" name="chart4" native="true">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>250</y>
<width>450</width>
<height>250</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1381</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>