Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Clipping QGraphicsItems in a QChart



  • Hi,

    I am using QChart for a real-time plot. The x-axis is a datetime axis, the y-axis is a value axis. As you can expect with the real-time plot, the window moves right on the x-axis ahead of time, and up/down on the y-axis as the max/min values change.

    As the window moves, the line series objects, managed by the QChart, are correctly painted inside the plotArea. But as I add custom QGraphicsItems to the scene, they can be partially painted outside the plotArea, because the scene area is bigger than the QChart's plotArea.

    An obvious solution would be to use clipping, and it worked good during my test. I've reimplemented the paint() method for my custom QGraphicsItems and used setClipRect() method on the painter with the QChart's plotArea rect. But what bothers me in this approach is that, whenever I read about clipping in the docs, it says that this operation is expensive and affects performance.

    I wonder, how expensive is it? Does clipping affects performance in some specific scenarios only, or should it be avoided in general? I've seen the warning in the docs, that it should not be used, if possible, but there is no further details.

    I also looked at the QCharts source, and I can see painter->setClipRect(clipRect) in the LineChartItem::paint(), so, I suppose, QChart is using exactly the same approach for keeping graphics objects inside the plot area. This sort of contradicts the recomendation regarding clipping in the docs, isn't it?

    If not clipping, I've also been playing with managing the custom QGraphicsItems myself and hiding them, when the item's coordinates go outside of QChart's plotArea, but this requires more code, might be more error-prone, and might not necessarilly be faster as the number of items grows.

    I would appreciate any thoughts on using clipping and/or ideas regarding alternative approaches for painting QGraphicsItems inside the QChart's plotArea.

    Regards,
    Yuri.


  • Lifetime Qt Champion

    Hi,

    This is something you have to benchmark for your application.

    If it's working OK for you then just use it.



  • For sure it can be measured, and I do agree with the approach "if it works fine, just use it".

    But I am still curious, why clipping is expensive in the first place, so that the warning exists in the docs? Has anyone experienced performance impact while actively using clipping for real-time chart?