如何刷新 QGraphicsView 以显示 QGraphicsScene 背景的变化

How to refresh QGraphicsView to show changes in the QGraphicsScene's background

我有一个自定义 QGraphicsViewQGraphicsScene。在 QGraphicsScene 中,我已经覆盖了 void drawBackground(QPainter *painter, const QRectF &rect) 并且基于我想打开和关闭网格的布尔标志。我尝试在我的函数中调用 clear() 或调用 paintereraseRect(sceneRect()) 但它没有用。所以在做了一些阅读之后,我猜它不应该工作,因为在更改场景后你需要刷新视图。这就是为什么我发出一个叫做 signalUpdateViewport()

的信号
void Scene::drawBackground(QPainter *painter, const QRectF &rect) {
    if(this->gridEnabled) {
        // Draw grid
    }
    else {
        // Erase using the painter
        painter->eraseRect(sceneRect());
        // or by calling
        //clear();
    }

    // Trigger refresh of view
    emit signalUpdateViewport();
    QGraphicsScene::drawBackground(painter, rect);
}

然后由我的视图捕获:

void View::slotUpdateViewport() {
    this->viewport()->update();
}

不用说这没有用。 不起作用 我的意思是结果(无论是从场景内部还是视图内部刷新)仅在更改小部件时可见,例如触发调整大小事件。

如何正确刷新场景视图以显示我在场景背景中所做的更改?

代码:

scene.h

#ifndef SCENE_HPP
#define SCENE_HPP

#include <QGraphicsScene>

class View;

class Scene : public QGraphicsScene
{
    Q_OBJECT
    Q_ENUMS(Mode)
    Q_ENUMS(ItemType)
  public:
    enum Mode { Default, Insert, Select };
    enum ItemType { None, ConnectionCurve, ConnectionLine, Node };

    Scene(QObject* parent = Q_NULLPTR);
    ~Scene();

    void setMode(Mode mode, ItemType itemType);
  signals:
    void signalCursorCoords(int x, int y);

    void signalSceneModeChanged(Scene::Mode sceneMode);
    void signalSceneItemTypeChanged(Scene::ItemType sceneItemType);
    void signalGridDisplayChanged(bool gridEnabled);

    void signalUpdateViewport();
  public slots:
    void slotSetSceneMode(Scene::Mode sceneMode);
    void slotSetSceneItemType(Scene::ItemType sceneItemType);

    void slotSetGridStep(int gridStep);
    void slotToggleGrid(bool flag);
  private:
    Mode sceneMode;
    ItemType sceneItemType;

    bool gridEnabled;
    int gridStep;

    void makeItemsControllable(bool areControllable);
    double round(double val);
  protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    void keyPressEvent(QKeyEvent *event);

    void drawBackground(QPainter *painter, const QRectF &rect);
};

Q_DECLARE_METATYPE(Scene::Mode)
Q_DECLARE_METATYPE(Scene::ItemType)

#endif // SCENE_HPP

scene.cpp

#include <QGraphicsItem>
#include <QGraphicsView>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QRectF>
#include <QKeyEvent>

#include <QtDebug>

#include "scene.h"

Scene::Scene(QObject* parent)
    : QGraphicsScene (parent),
      sceneMode(Default),
      sceneItemType(None),
      gridEnabled(true),
      gridStep(30)
{

}

Scene::~Scene()
{
}

void Scene::setMode(Mode mode, ItemType itemType)
{
  this->sceneMode = mode;
  this->sceneItemType = itemType;

  QGraphicsView::DragMode vMode = QGraphicsView::NoDrag;

  switch(mode) {
    case Insert:
    {
      makeItemsControllable(false);
      vMode = QGraphicsView::NoDrag;
      break;
    }
    case Select:
    {
      makeItemsControllable(true);
      vMode = QGraphicsView::RubberBandDrag;
      break;
    }
    case Default:
    {
      makeItemsControllable(false);
      vMode = QGraphicsView::NoDrag;
      break;
    }
  }

  QGraphicsView* mView = views().at(0);
  if(mView) {
    mView->setDragMode(vMode);
  }
}

void Scene::slotSetSceneMode(Scene::Mode sceneMode)
{
  this->sceneMode = sceneMode;
  qDebug() << "SM" << (int)this->sceneMode;
  emit signalSceneModeChanged(this->sceneMode);
}

void Scene::slotSetSceneItemType(Scene::ItemType sceneItemType)
{
  this->sceneItemType = sceneItemType;
  qDebug() << "SIT:" << (int)this->sceneItemType;
  emit signalSceneItemTypeChanged(this->sceneItemType);
}

void Scene::slotSetGridStep(int gridStep)
{
  this->gridStep = gridStep;
}

void Scene::slotToggleGrid(bool flag)
{
  this->gridEnabled = flag;
  invalidate(sceneRect(), BackgroundLayer);
  qDebug() << "Grid " << (this->gridEnabled ? "enabled" : "disabled");
  update(sceneRect());
  emit signalGridDisplayChanged(this->gridEnabled);
}

void Scene::makeItemsControllable(bool areControllable)
{
  foreach(QGraphicsItem* item, items()) {
    if(/*item->type() == QCVN_Node_Top::Type
       ||*/ item->type() == QGraphicsLineItem::Type
       || item->type() == QGraphicsPathItem::Type) {
      item->setFlag(QGraphicsItem::ItemIsMovable, areControllable);
      item->setFlag(QGraphicsItem::ItemIsSelectable, areControllable);
    }
  }
}

double Scene::round(double val)
{
  int tmp = int(val) + this->gridStep/2;
  tmp -= tmp % this->gridStep;
  return double(tmp);
}

void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
  QGraphicsScene::mousePressEvent(event);
}

void Scene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
  emit signalCursorCoords(int(event->scenePos().x()), int(event->scenePos().y()));
  QGraphicsScene::mouseMoveEvent(event);
}

void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
  QGraphicsScene::mouseReleaseEvent(event);
}

void Scene::keyPressEvent(QKeyEvent* event)
{
  if(event->key() == Qt::Key_M) {
    slotSetSceneMode(static_cast<Mode>((int(this->sceneMode) + 1) % 3));
  }
  else if(event->key() == Qt::Key_G) {
    slotToggleGrid(!this->gridEnabled);
  }

  QGraphicsScene::keyPressEvent(event);
}

void Scene::drawBackground(QPainter *painter, const QRectF &rect)
{
  // FIXME Clearing and drawing the grid happens only when scene size or something else changes
  if(this->gridEnabled) {
    painter->setPen(QPen(QColor(200, 200, 255, 125)));
    // draw horizontal grid
    double start = round(rect.top());
    if (start > rect.top()) {
      start -= this->gridStep;
    }
    for (double y = start - this->gridStep; y < rect.bottom(); ) {
      y += this->gridStep;
      painter->drawLine(int(rect.left()), int(y), int(rect.right()), int(y));
    }
    // now draw vertical grid
    start = round(rect.left());
    if (start > rect.left()) {
      start -= this->gridStep;
    }
    for (double x = start - this->gridStep; x < rect.right(); x += this->gridStep) {
      painter->drawLine(int(x), int(rect.top()), int(x), int(rect.bottom()));
    }
  }
  else {
    // Erase whole scene's background
    painter->eraseRect(sceneRect());
  }

  slotToggleGrid(this->gridEnabled);

  QGraphicsScene::drawBackground(painter, rect);
}

View 当前不包含任何内容 - 都是默认值。我的 SceneView 实例在我的 QMainWindow 中的设置如下:

void App::initViewer()
{
  this->scene = new Scene(this);
  this->view = new View(this->scene, this);
  this->viewport = new QGLWidget(QGLFormat(QGL::SampleBuffers), this->view);
  this->view->setRenderHints(QPainter::Antialiasing);
  this->view->setViewport(this->viewport);
  this->view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
  this->view->setCacheMode(QGraphicsView::CacheBackground);

  setCentralWidget(this->view);
}

编辑: 我尝试在 update()clear() 或任何我之前调用 invalidate(sceneRect(), BackgroundLayer)试图触发刷新。

我也在场景中尝试了 QGraphicsScene::update(),但它没有用 将两者都没有参数传递给函数调用,然后传递 sceneRect() 并没有导致任何与我不同的结果如上所述。

当您更改网格设置时,无论是打开还是关闭(或颜色等),您都需要调用 QGraphicsScene::update。这也是一个插槽,因此您可以根据需要将信号连接到它。您还可以指定暴露区域;如果你不这样做,那么它会使用所有的默认值。对于网格,这可能就是您想要的。

您不需要清除网格。更新调用确保更新的区域被清除,然后如果你想要网格,你可以在上面绘制,或者如果网格不应该在那里,则不要在上面绘制。

发现问题 - 我忘记设置场景矩形的大小:

this->scene->setSceneRect(QRectF(QPointF(-1000, 1000), QSizeF(2000, 2000)));

我实际上是通过决定打印 sceneRect() 调用返回的 QRectF 的大小来发现问题的,当我查看输出时我看到了 0, 0 所以基本上我是确实触发了更新,但在面积为 0 的场景中(显然)不会产生任何结果。

我测试和工作的另一件事是删除我的视图的后台缓存。

有时 view/scene 直接拒绝 运行 update(),在 update() 修复它之前处理应用程序事件,如下所示:

qApp->processEvents();
update();