It appears that my previous attempt was terribly complicated while the solution is in fact very easy.
Instead of the previous code it was enough to write:
qgv->ensureVisible(itemUnderCursor);
and make sure that sceneRect()
will not be set with any value, but rather left to be handled by the scene itself.
This allowed the scene to automatically adjust its size accordingly to the items on it and forced the scrollbars to follow the moving item when outside the visible rectangle of QGraphicsView
.