Force repaint of entire viewport of QGraphicsScene
<p>I need to force a repaint of a <code>QGraphicsScene</code> inside a view - the entire view.<br>
I am explaining below why I can't repaint specific items, and what I have tried.</p>

<p>I have a <code>QGraphicsScene</code> subclass that paints an "actual canvas rectangle" for items - but items can be placed outside this rectangle, or be larger.<br>
The "actual canvas rectangle" can be resized, and I also move it on resize so its <code>(0,0)</code> is always the point of <code>(0,0)</code> coordinates in <code>QGraphicsScene</code>.<br>
I made the resize smart enough to repaint the largest rectangle between the old and new canvas.<br>
I did not consider a redraw on the entire <code>QGraphicsScene</code> or any items outside the canvas, because it is giant - and the items could be all over the place - and because the initial application was also re-centering the canvas in view.</p>

<p>This class is referenced from a <code>QWidget</code> created next to a <code>QScrollArea</code> promoted to the <code>QGraphicsView</code> subclass containing the scene.</p>

<p>If I place a large text item on the scene, and I resize the canvas, I can have leftover pieces of text either on the left or the right (depending if the item was larger than the old or new canvas). Because of the "move" generally increasing the canvas size leaves an artifact on the left.</p>

<p>Making movements on canvas, or zooming, does a repaint - but even paning the scene doesn't.</p>

<p>I need to force a repaint on the <strong>old</strong> item, after the resize which technically moves it. </p>

<p>I would be happy with a viewport update, but I can't get it: a few attempts:</p>

<pre><code>    QList&lt;QGraphicsView*&gt; views = m_canvas-&gt;views();
    if(views.size() &gt; 0)
    {
        m_canvas-&gt;update(views.at(0)-&gt;rect());  
        // or
        QWidget* viewport = views.at(0)-&gt;viewport();
        viewport-&gt;update();
        // or
        QRectF r = views.at(0)-&gt;mapToScene(views.at(0)-&gt;rect()).boundingRect();
        m_canvas-&gt;update(r);
    }
</code></pre>

<p>None of these works.</p>

<p>Since the coordinates have changed, I don't even know how to map the old bounding rectangle of my item to the new coordinates of the scene.<br>
That's why my thought is, simpler to update the view.<br>
But I must be doing something wrong since the leftover drawing is still there.</p>

<p>All I really want is to force a scene redraw on the entire viewport.<br>
How can I do that ?</p>

<p>Edit - adding a program that reproduces the issue.</p>

<blockquote>
  <p>myview.h</p>
</blockquote>

<pre><code>#ifndef MYVIEW_H
#define MYVIEW_H

#include &lt;QGraphicsView&gt;
#include &lt;QGraphicsScene&gt;

class MyView : public QGraphicsView
{
public:
    MyView(QWidget *parent = 0) : QGraphicsView(parent) {
        setRenderHints(QPainter::Antialiasing |
                       QPainter::SmoothPixmapTransform);
        setCacheMode(CacheNone); // to avoid smear when panning very far
        setAutoFillBackground(true);
        setBackgroundRole(QPalette::Window);
        setViewportUpdateMode(SmartViewportUpdate);
        setTransformationAnchor(AnchorUnderMouse);
        setDragMode(ScrollHandDrag);
    }
};

class MyScene : public QGraphicsScene
{
public:
    explicit MyScene(const qreal sceneW, const qreal sceneH,
            const qreal canvasW, const qreal canvasH,
            QObject *parent = 0) : QGraphicsScene(parent) {
        m_actualCanvasRect = QRectF(0, 0, canvasW, canvasH);
        setSceneRect(0.5*(canvasW - sceneW), 0.5*(canvasH - sceneH), sceneW, sceneH);
    }

    void setCanvasSize(const qreal canvasW, const qreal canvasH) {
        setSceneRect(0.5*(canvasW - sceneRect().width()),
                     0.5*(canvasH - sceneRect().height()),
                     sceneRect().width(), sceneRect().height());
        qreal oldW = m_actualCanvasRect.width(),
              oldH = m_actualCanvasRect.height();
        m_actualCanvasRect = QRectF(0, 0, canvasW, canvasH);
        qreal updateW = qMax(oldW, canvasW);
        qreal updateH = qMax(oldH, canvasH);
        QRectF updateRect(0, 0, updateW, updateH);
        update(updateRect);
    }

protected:
    virtual void drawBackground(QPainter *painter, const QRectF &amp;rect) {
        QGraphicsScene::drawBackground(painter, rect);
        QPen p(Qt::green);
        p.setWidth(5);   // this creates artifacts but lucky in my code i don't use a width
        painter-&gt;setPen(p);
        painter-&gt;setBrush(Qt::red);
        painter-&gt;drawRect(m_actualCanvasRect);
    }

private:
    QRectF m_actualCanvasRect;
};

#endif // MYVIEW_H
</code></pre>

<blockquote>
  <p>testscene.h</p>
</blockquote>

<pre><code>#ifndef TESTSCENE_H
#define TESTSCENE_H

#include &lt;QWidget&gt;
#include "myview.h"

namespace Ui {
class TestScene;
class MyScene;
class QGraphicsTextItem;
}

class TestScene : public QWidget
{
    Q_OBJECT

public:
    explicit TestScene(QWidget *parent = 0);
    ~TestScene();

private slots:
    void on_btnResize_clicked();

private:
    Ui::TestScene *ui;
    MyScene* s;
    QGraphicsTextItem* yyy;
};

#endif // TESTSCENE_H
</code></pre>

<blockquote>
  <p>main.cpp</p>
</blockquote>

<pre><code>#include "testscene.h"
#include &lt;QApplication&gt;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TestScene w;
    w.show();

    return a.exec();
}
</code></pre>

<blockquote>
  <p>testscene.cpp</p>
</blockquote>

<pre><code>#include "testscene.h"
#include "ui_testscene.h"

#include &lt;QGraphicsTextItem&gt;

TestScene::TestScene(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TestScene)
{
    ui-&gt;setupUi(this);

    qreal w = 300, h = 200;
    s = new MyScene(1000000, 1000000, w, h);
    ui-&gt;myView-&gt;setScene(s);

    yyy = new QGraphicsTextItem("Hello Great World");
    QFont fff("Arial", 34);
    yyy-&gt;setFont(fff);

    QSizeF sz = yyy-&gt;boundingRect().size();
    qreal cx = (w - sz.width())/2;
    qreal cy = (h - sz.height())/2;
    yyy-&gt;setPos(cx, cy);

    s-&gt;addItem(yyy);
}

TestScene::~TestScene()
{
    delete ui;
}

void TestScene::on_btnResize_clicked()
{
    bool ok;
    qreal w = ui-&gt;linWidth-&gt;text().toDouble(&amp;ok);
    if(!ok || w &lt;= 0) return;
    qreal h = ui-&gt;linHeight-&gt;text().toDouble(&amp;ok);
    if(!ok || h &lt;= 0) return;

    s-&gt;setCanvasSize(w, h);

    QSizeF sz = yyy-&gt;boundingRect().size();
    qreal cx = (w - sz.width())/2;
    qreal cy = (h - sz.height())/2;
    yyy-&gt;setPos(cx, cy);
}
</code></pre>

<blockquote>
  <p>testscene.ui</p>
</blockquote>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;ui version="4.0"&gt;
 &lt;class&gt;TestScene&lt;/class&gt;
 &lt;widget class="QWidget" name="TestScene"&gt;
  &lt;property name="geometry"&gt;
   &lt;rect&gt;
    &lt;x&gt;0&lt;/x&gt;
    &lt;y&gt;0&lt;/y&gt;
    &lt;width&gt;649&lt;/width&gt;
    &lt;height&gt;307&lt;/height&gt;
   &lt;/rect&gt;
  &lt;/property&gt;
  &lt;property name="windowTitle"&gt;
   &lt;string&gt;TestScene&lt;/string&gt;
  &lt;/property&gt;
  &lt;widget class="QScrollArea" name="scrlArea"&gt;
   &lt;property name="geometry"&gt;
    &lt;rect&gt;
     &lt;x&gt;60&lt;/x&gt;
     &lt;y&gt;40&lt;/y&gt;
     &lt;width&gt;471&lt;/width&gt;
     &lt;height&gt;231&lt;/height&gt;
    &lt;/rect&gt;
   &lt;/property&gt;
   &lt;property name="lineWidth"&gt;
    &lt;number&gt;0&lt;/number&gt;
   &lt;/property&gt;
   &lt;property name="verticalScrollBarPolicy"&gt;
    &lt;enum&gt;Qt::ScrollBarAlwaysOff&lt;/enum&gt;
   &lt;/property&gt;
   &lt;property name="horizontalScrollBarPolicy"&gt;
    &lt;enum&gt;Qt::ScrollBarAlwaysOff&lt;/enum&gt;
   &lt;/property&gt;
   &lt;property name="widgetResizable"&gt;
    &lt;bool&gt;true&lt;/bool&gt;
   &lt;/property&gt;
   &lt;widget class="QWidget" name="scrollAreaWidgetContents"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;0&lt;/x&gt;
      &lt;y&gt;0&lt;/y&gt;
      &lt;width&gt;469&lt;/width&gt;
      &lt;height&gt;229&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
    &lt;widget class="MyView" name="myView"&gt;
     &lt;property name="geometry"&gt;
      &lt;rect&gt;
       &lt;x&gt;10&lt;/x&gt;
       &lt;y&gt;10&lt;/y&gt;
       &lt;width&gt;451&lt;/width&gt;
       &lt;height&gt;211&lt;/height&gt;
      &lt;/rect&gt;
     &lt;/property&gt;
     &lt;property name="lineWidth"&gt;
      &lt;number&gt;0&lt;/number&gt;
     &lt;/property&gt;
     &lt;property name="verticalScrollBarPolicy"&gt;
      &lt;enum&gt;Qt::ScrollBarAlwaysOff&lt;/enum&gt;
     &lt;/property&gt;
     &lt;property name="horizontalScrollBarPolicy"&gt;
      &lt;enum&gt;Qt::ScrollBarAlwaysOff&lt;/enum&gt;
     &lt;/property&gt;
    &lt;/widget&gt;
   &lt;/widget&gt;
  &lt;/widget&gt;
  &lt;widget class="QPushButton" name="btnResize"&gt;
   &lt;property name="geometry"&gt;
    &lt;rect&gt;
     &lt;x&gt;550&lt;/x&gt;
     &lt;y&gt;50&lt;/y&gt;
     &lt;width&gt;75&lt;/width&gt;
     &lt;height&gt;23&lt;/height&gt;
    &lt;/rect&gt;
   &lt;/property&gt;
   &lt;property name="text"&gt;
    &lt;string&gt;Resize&lt;/string&gt;
   &lt;/property&gt;
  &lt;/widget&gt;
  &lt;widget class="QLineEdit" name="linWidth"&gt;
   &lt;property name="geometry"&gt;
    &lt;rect&gt;
     &lt;x&gt;540&lt;/x&gt;
     &lt;y&gt;90&lt;/y&gt;
     &lt;width&gt;113&lt;/width&gt;
     &lt;height&gt;20&lt;/height&gt;
    &lt;/rect&gt;
   &lt;/property&gt;
  &lt;/widget&gt;
  &lt;widget class="QLineEdit" name="linHeight"&gt;
   &lt;property name="geometry"&gt;
    &lt;rect&gt;
     &lt;x&gt;540&lt;/x&gt;
     &lt;y&gt;130&lt;/y&gt;
     &lt;width&gt;113&lt;/width&gt;
     &lt;height&gt;20&lt;/height&gt;
    &lt;/rect&gt;
   &lt;/property&gt;
  &lt;/widget&gt;
 &lt;/widget&gt;
 &lt;layoutdefault spacing="6" margin="11"/&gt;
 &lt;customwidgets&gt;
  &lt;customwidget&gt;
   &lt;class&gt;MyView&lt;/class&gt;
   &lt;extends&gt;QGraphicsView&lt;/extends&gt;
   &lt;header location="global"&gt;myview.h&lt;/header&gt;
  &lt;/customwidget&gt;
 &lt;/customwidgets&gt;
 &lt;resources/&gt;
 &lt;connections/&gt;
&lt;/ui&gt;
</code></pre>

<p>To test - after it starts, set a size 40,30 and see artifacts. Set larger size after moving the program, artifacts will show again.</p>

<p>I have realized that I can do the resize after repositioning the item - and that takes care of artifacts for some odd reason (even though the update called in resize does not include the entire item bounding rectangle). That conflicts with some other operations, but I can make it work.</p>

<p>So - I am not desperate for a solution anymore, just understanding. Why / how can I force a redraw in the current situation - with an item <code>setPos</code> after the resize.</p>