diff options
Diffstat (limited to 'examples/widgets/painting/plot')
-rw-r--r-- | examples/widgets/painting/plot/doc/plot.png | bin | 0 -> 13030 bytes | |||
-rw-r--r-- | examples/widgets/painting/plot/doc/plot.rst | 36 | ||||
-rw-r--r-- | examples/widgets/painting/plot/plot.py | 60 |
3 files changed, 47 insertions, 49 deletions
diff --git a/examples/widgets/painting/plot/doc/plot.png b/examples/widgets/painting/plot/doc/plot.png Binary files differnew file mode 100644 index 000000000..e5031e351 --- /dev/null +++ b/examples/widgets/painting/plot/doc/plot.png diff --git a/examples/widgets/painting/plot/doc/plot.rst b/examples/widgets/painting/plot/doc/plot.rst new file mode 100644 index 000000000..a63eaed87 --- /dev/null +++ b/examples/widgets/painting/plot/doc/plot.rst @@ -0,0 +1,36 @@ +Plot Example +============ + +The Plot example shows how to display a graph from data using an +`opaque container <https://doc.qt.io/qtforpython-6/shiboken6/typesystem_containers.html>`_. + +It draws an sine graph using ``QPainter.drawPolyline()`` from a list of points. +The list of points is continuously updated, as is the case for a example for a +graph of an oscilloscope or medical patient monitor. +In this case, it makes sense from a performance point of view to avoid the +conversion of a Python list of data to a C++ list (``QList<QPoint>``) +for each call to the plot function ``QPainter.drawPolyline()``. +This is where opaque containers come into play. + +Instead of Python list of points, a ``QPointList`` is instantiated to store +the data. ``QPointList`` is an opaque container wrapping a ``QList<QPoint>``. +It can be passed to ``QPainter.drawPolyline()`` instead of a Python list of +points. + +The type is declared in the entry for the ``QList`` container type in the +type system file of the ``QtCore`` library: + +.. code-block:: xml + + <container-type name="QList" type="list" + opaque-containers="int:QIntList;QPoint:QPointList;QPointF:QPointFList"> + ... + </container-type> + +In the ``shift()`` member function, new data are appended to the list while +old data moving out of the visible window are removed from the front of the +list. + +.. image:: plot.png + :width: 400 + :alt: Plot Screenshot diff --git a/examples/widgets/painting/plot/plot.py b/examples/widgets/painting/plot/plot.py index 156a6408d..d437309d0 100644 --- a/examples/widgets/painting/plot/plot.py +++ b/examples/widgets/painting/plot/plot.py @@ -1,50 +1,13 @@ -############################################################################# -## -## Copyright (C) 2021 The Qt Company Ltd. -## Contact: https://www.qt.io/licensing/ -## -## This file is part of the Qt for Python examples of the Qt Toolkit. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +from __future__ import annotations import math import sys from PySide6.QtWidgets import QWidget, QApplication -from PySide6.QtCore import QPoint, QRect, QTimer, Qt, Slot -from PySide6.QtGui import (QColor, QPainter, QPaintEvent, QPen, QPointList, - QTransform) +from PySide6.QtCore import QPoint, QRect, QTimer, Qt +from PySide6.QtGui import QPainter, QPointList WIDTH = 680 @@ -63,6 +26,7 @@ class PlotWidget(QWidget): self._timer.timeout.connect(self.shift) self._points = QPointList() + self._points.reserve(WIDTH) self._x = 0 self._delta_x = 0.05 self._half_height = HEIGHT / 2 @@ -87,13 +51,11 @@ class PlotWidget(QWidget): self.update() def paintEvent(self, event): - painter = QPainter() - painter.begin(self) - rect = QRect(QPoint(0, 0), self.size()) - painter.fillRect(rect, Qt.white) - painter.translate(-self._points[0].x(), 0) - painter.drawPolyline(self._points) - painter.end() + with QPainter(self) as painter: + rect = QRect(QPoint(0, 0), self.size()) + painter.fillRect(rect, Qt.white) + painter.translate(-self._points[0].x(), 0) + painter.drawPolyline(self._points) if __name__ == "__main__": |