QChart Line越界时window最先开始显示?

QChart Line exceed the boundary when the window first start to show?

在我的应用程序中,我需要在用户移动鼠标时画一条线。但是当图表 window 首次显示并移动鼠标时,线条超出了边界。我调整大小 window,它工作正常。不知道为什么先启动window 不正常,好像前后没有区别

密码是:

import random
from PyQt5.QtChart import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class DemoChar(QChartView):
    def __init__(self):
        super().__init__()
        self.setRenderHint(QPainter.Antialiasing)
        self.chart = QChart()
        self.chart.setTitle('Demo')
        self.chart.setAnimationOptions(QChart.SeriesAnimations)
        self.setChart(self.chart)
        self.lineItem = QGraphicsLineItem(self.chart)

        series = QLineSeries(name="random serie")
        series.setPointsVisible(True)

        for i in range(20):
            series << QPointF(0.1 * i, random.uniform(-10, 10))

        self.chart.addSeries(series)
        self.chart.createDefaultAxes()
        axis_x, axis_y = self.chart.axes()

        #get axis min, max value to calculate position
        self.min_x, self.min_y = axis_x.min(), axis_y.min()
        self.max_x, self.max_y = axis_x.max(), axis_y.max()

    def resizeEvent(self, event):
        super().resizeEvent(event)

        self.point_bottom = self.chart.mapToPosition(QPointF(self.min_x, self.min_y))
        self.point_top = self.chart.mapToPosition(QPointF(self.min_x, self.max_y))

        line = self.lineItem.line()
        line.setLine(line.x1(), self.point_bottom.y(), line.x2(), self.point_top.y() )
        self.lineItem.setLine(line)

    def mouseMoveEvent(self, event):
        super().mouseMoveEvent(event)
        pt = self.chart.mapToValue(event.pos())

        if not (self.min_x <= pt.x() <= self.max_x):
            self.lineItem.hide()
            return

        for marker in self.chart.legend().markers():
            if marker.series().isVisible():
                points = marker.series().pointsVector() #type:list
                for point in points:
                    left_idx = points.index(point)
                    right_point = points[left_idx + 1]
                    if point.x() <= pt.x() <= right_point.x():
                        left_delta = pt.x() - point.x()
                        right_delta = right_point.x() - pt.x()

                        if left_delta < right_delta:
                            pos = self.chart.mapToPosition(point)
                            self.lineItem.setLine(pos.x(), self.point_bottom.y(), pos.x(), self.point_top.y() )
                            self.lineItem.show()
                        break

app = QApplication([])
demo = DemoChar()
demo.show()
app.exec()

首先像这样启动和移动鼠标。

然后调整大小window,它工作正常。

painted和resizeEvent之间好像有差距,可能是bug。解决方法是通过代码修改大小:

# ...
self.min_x, self.min_y = axis_x.min(), axis_y.min()
self.max_x, self.max_y = axis_x.max(), axis_y.max()

<b>self.resize(self.size() + QSize(1, 1))</b>