Programmatic scrolling with QGraphicsView and QGraphicsItem?
<p>I would like to programmatically scroll a scene to the left / right, but I am not sure how to do that properly. Note that I do <strong>not</strong> want to have (visible) scroll bars.</p>

<p>I use a standard <code>QGraphicsView</code> + <code>QGraphicsScene</code> + <code>QGraphicsItem</code> setup. I have downsized it to the minimum, with one single <code>QGraphicsItem</code> (a <code>QGraphicsRectItem</code>) in the scene.</p>

<p>I have managed to achieve programmatic scrolling by setting my view like this:</p>

<pre><code>// view setup
view-&gt;setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view-&gt;setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
</code></pre>

<p>and then, in another part of the code:</p>

<pre><code>// programmatic scrolling
QScrollBar* const sb = view-&gt;horizontalScrollBar();
sb-&gt;setRange(0, 1000); // some values for experimenting
sb-&gt;setValue(sb-&gt;value() + 100 or -100); // some increment for experimenting
</code></pre>

<p>This works, but... scrolling through invisible scrollbars doesn't feel right.</p>

<p>I tried this more straightforward approach:</p>

<pre><code>// programmatic scrolling - doesn't quite work
view-&gt;viewport()-&gt;scroll(100 or -100, 0); // some increment for experimenting
</code></pre>

<p>This code does scroll, but when the rectangle goes off the left edge of the view, and I reverse the scrolling direction (increment changed from <code>100</code> to <code>-100</code> in the call to <code>scroll()</code>), the uncovered part of the rectangle is not repainted. The reason is that <code>QGraphicsRectItem::paint()</code> is not called in that case (it is called when using the scrollbar method).</p>

<p>So, is there a way to get <code>viewport()-&gt;scroll()</code> work? Or some other simple way to achieve programmatic scrolling? Or is the artificial scrollbar method just the way to go?</p>