最近,我发现我的 QChart 图例可以分离并放入另一个独立的小部件中。Qt 文档说 QLegend 类方法 QLegend::detachFromChart() 可以将图例与图表分开。
不幸的是,当我尝试将图例添加到另一个小部件布局时,出现以下错误:
Traceback (most recent call last):
File "/home/artem/.local/lib/python3.6/site-packages/shiboken2/files.dir/shibokensupport/signature/loader.py", line 111, in seterror_argument
return errorhandler.seterror_argument(args, func_name)
File "/home/artem/.local/lib/python3.6/site-packages/shiboken2/files.dir/shibokensupport/signature/errorhandler.py", line 97, in seterror_argument
update_mapping()
File "/home/artem/.local/lib/python3.6/site-packages/shiboken2/files.dir/shibokensupport/signature/mapping.py", line 240, in update
top = __import__(mod_name)
File "/home/artem/.local/lib/python3.6/site-packages/numpy/__init__.py", line 142, in <module>
from . import core
File "/home/artem/.local/lib/python3.6/site-packages/numpy/core/__init__.py", line 67, in <module>
raise ImportError(msg.format(path))
ImportError: Something is wrong with the numpy installation. While importing we detected an older version of numpy in ['/home/artem/.local/lib/python3.6/site-packages/numpy']. One method of fixing this is to repeatedly uninstall numpy until none is found, then reinstall this version.
Fatal Python error: seterror_argument did not receive a result
Current thread 0x00007efc67a96740 (most recent call first):
File "/home/artem/\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b/Projects/QtChartsExamples/test/main.py", line 20 in <module>
这是一个简单的例子:
from PySide2 import QtGui, QtWidgets, QtCore
from PySide2.QtCharts import QtCharts
from psutil import cpu_percent, cpu_count
import sys
import random
class cpu_chart(QtCharts.QChart):
def __init__(self, parent=None):
super().__init__(parent)
self.legend().setAlignment(QtCore.Qt.AlignLeft)
self.legend().setContentsMargins(0.0, 0.0, 5.0, 0.0)
self.legend().setMarkerShape(QtCharts.QLegend.MarkerShapeCircle)
self.legend().detachFromChart()
self.axisX = QtCharts.QValueAxis()
self.axisY = QtCharts.QValueAxis()
self.axisX.setVisible(False)
self.x = 0
self.y = 0
self.percent = cpu_percent(percpu=True)
for i in range(cpu_count()):
core_series = QtCharts.QSplineSeries()
core_series.setName(f"CPU {i+1}: {self.percent[i]: .1f} %")
colour = [random.randrange(0, 255),
random.randrange(0, 255),
random.randrange(0, 255)]
pen = QtGui.QPen(QtGui.QColor(colour[0],
colour[1],
colour[2])
)
pen.setWidth(1)
core_series.setPen(pen)
core_series.append(self.x, self.y)
self.addSeries(core_series)
self.addAxis(self.axisX, QtCore.Qt.AlignBottom)
self.addAxis(self.axisY, QtCore.Qt.AlignLeft)
for i in self.series():
i.attachAxis(self.axisX)
i.attachAxis(self.axisY)
self.axisX.setRange(0, 100)
self.axisY.setTickCount(5)
self.axisY.setRange(0, 100)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
chart = cpu_chart()
chart.setAnimationOptions(QtCharts.QChart.SeriesAnimations)
chart_view = QtCharts.QChartView(chart)
chart_view.setRenderHint(QtGui.QPainter.Antialiasing)
container = QtWidgets.QWidget()
hbox = QtWidgets.QHBoxLayout()
hbox.addWidget(chart.legend())
hbox.addWidget(chart_view)
container.setLayout(hbox)
window.setCentralWidget(container)
window.resize(400, 300)
window.show()
sys.exit(app.exec_())
弄清楚这有什么问题会很有趣。我真的可以这样做吗?