Search code examples
pythonpyqtpyqt5qtchartsqchart

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


In my app, i need to draw a line when user move mouse. But when the chart window first showing and move mouse, the line exceed boundary. The i resize window, it work ok. i don't know why first start window it not work fine, it's seem that there is no different the first and after.

The code is:

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()

First start and move mouse like this.

1

Then resize window, it work ok.

2


Solution

  • There seems to be a gap between the painted and the resizeEvent, probably a bug. A workaround is to modify the size by code:

    # ...
    self.min_x, self.min_y = axis_x.min(), axis_y.min()
    self.max_x, self.max_y = axis_x.max(), axis_y.max()
    
    self.resize(self.size() + QSize(1, 1))