【问题标题】:Adding multiple QGraphicsScene on QGraphicsView在 QGraphicsView 上添加多个 QGraphicsScene
【发布时间】:2020-07-01 08:19:46
【问题描述】:

我正在创建一个用户需要使用 QGraphicsView 进行交互的 GUI。 所以我现在正在做的是,我创建了 QGraphicsScene 并将其分配给 QGraphicsView。

应该有两层用于绘制:一层是静态的,一层是动态的。

静态层应该具有在启动时创建一次的对象,而动态层包含多个项目(可能是数百个)并且用户将与动态层对象进行交互。

目前我在同一场景上绘制两个图层,由于绘制了大量对象而产生了一些滞后。

所以问题:有没有办法将两个或多个 QGraphicsScene 分配给 QGraphicsView ?

【问题讨论】:

  • 恐怕 Qt 开发人员明确打算每个视图有一个场景。您不能使用专用的QGraphicsItem 实例来分隔场景的重要部分吗? AFAIK,场景图的设计通常利用层次结构来最小化单个节点变化对性能的影响。或者,您可以将静态场景渲染为像素图,并首先在场景的动态部分“下方”渲染像素图。 Bitblitting 像素图可能比重新渲染便宜得多并获得缺失的性能。
  • 我想到了另一个使用 OpenCV 创建多个 Mat 图像的方法,最后将它们全部进行 ORing 并将它们放在场景中。还没做,你的解决方案和这个一样吗?
  • 也许我可以使用 qgraphicsitem 的透明度 graphicsEffect 来达到这个目的,但我还没有测试过。

标签: qt qgraphicsview qgraphicsscene qgraphicsitem qgraphicswidget


【解决方案1】:

一种选择可能是实现您自己的从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();

我只进行了基本测试,但它的表现似乎符合预期。但不确定是否存在任何潜在的性能问题。

【讨论】:

    猜你喜欢
    • 2017-02-16
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多