Adding multiple QGraphicsScene on QGraphicsView

1.5k views Asked by At

I am creating a GUI where user needs to interact using QGraphicsView. So what I am doing right now is, I created QGraphicsScene and assigned it to QGraphicsView.

There are supposed to be two layers for drawing: one static and one dynamic.

Static layer is supposed to have objects that are created once at startup while dynamic layer contains multiple items (may be hundred of them) and user will interact will dynamic layer objects.

Currently I am drawing both layers on same scene which creates some lag due to large number of objects being drawn.

So question: Is there any way to assign two or more QGraphicsScene to a QGraphicsView ?

1

There are 1 answers

0
G.M. On

One option might be to implement your own class derived from QGraphicsScene that can then render a second 'background' scene in its drawBackground override.

class graphics_scene: public QGraphicsScene {
  using super = QGraphicsScene;
public:
  using super::super;
  void set_background_scene (QGraphicsScene *background_scene)
    {
      m_background_scene = background_scene;
    }
protected:
  virtual void drawBackground (QPainter *painter, const QRectF &rect) override
    {
      if (m_background_scene) {
        m_background_scene->render(painter, rect, rect);
      }
    }
private:
  QGraphicsScene *m_background_scene = nullptr;
};

Then use as...

QGraphicsView view;

/*
 * fg is the 'dynamic' layer.
 */
graphics_scene fg;

/*
 * bg is the 'static' layer used as a background.
 */
QGraphicsScene bg;
bg.addText("Text Item 1")->setPos(50, 50);
bg.addText("Text Item 2")->setPos(250, 250);
fg.addText("Text Item 3")->setPos(50, 50);
fg.addText("Text Item 4")->setPos(350, 350);
view.setScene(&fg);
fg.set_background_scene(&bg);
view.show();

I've only performed basic testing but it appears to behave as expected. Not sure about any potential performance issues though.