一种选择可能是实现您自己的派生类QGraphicsScene
,然后可以在其drawBackground
覆盖中呈现第二个“背景”场景。
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;
};
然后用作...
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();
我只进行了基本测试,但它的表现似乎符合预期。但不确定任何潜在的性能问题。