Important: Please read the Qt Code of Conduct -

SetSceneRect doesn't seem to work (PyQt)

  • Hi,

    I'm having a hard time getting setSceneRect to work on a QGraphicsView Widget. Here's my program:

    @#!/usr/bin/env python

    -- coding: utf-8 --

    import sys, time, math

    from PyQt4.QtGui import QApplication, QGraphicsView, QGraphicsScene, QPainter
    from PyQt4.QtCore import QTimer
    from PyQt4.QtOpenGL import QGLWidget

    class App(QApplication):
    def init(self, args):
    QApplication.init(self, args)

    self._t0 = time.time()
    self._last_point = None
    self._scene = QGraphicsScene(self)
    self._view = QGraphicsView(self._scene)
    self._timer = QTimer(self)

    def _on_timeout(self):
    x = time.time() - self._t0
    y = math.sin(0.1 * x * 2 * math.pi)

    if self._last_point is not None:
      self._scene.addLine(self._last_point['x'], self._last_point['y'], x, y)
      self._view.setSceneRect(x - 20, -1, 20, 2)  # Show the last 20 seconds
    self._last_point = { 'x': x, 'y': y }

    if name == 'main':
    app = App(sys.argv)


    This creates a sine wave in a QGraphicsScene, and shows it using a QGraphicsView. The plan is to show the last 20 seconds and the full vertical range of the plot, so on line 33 I set the horizontal and vertical viewing range using setSceneRect on the QGraphicsView. But the graph is still minuscule in the center of the window. Can someone tell me what I'm doing wrong? Is there a better way to achieve this?

  • 5.4.2 still doesn't work

Log in to reply