QGraphicsScene::fitInView() only works on resizing
<p>I have a game that happens on top of a static map. I thought of painting the map in QGraphicsView::drawBackground().</p>

<p>All seems swell. Except that nothing is drawn unless I do a window resize... I think it has to do with <code>QGraphicsScene::fitInView()</code> or something related...</p>

<p>My <strong>mapscene.cpp</strong></p>

<pre><code>#include "mapscene.h"
#include &lt;qpainter.h&gt;
#include &lt;iostream&gt;

static const float WIDTH = 800.f;
static const float HEIGHT = 480.f;

static const float _map[][2] = {
    { 0, 0 },
    { 1, 1 },
//    { 1, 0 },  // TEMP: coordinates of map
//    { 0, 1 },
//    { 0, 0 },
};

MapScene::MapScene() : QGraphicsScene(0, 0, WIDTH, HEIGHT)
{
    mapPath.moveTo(_map[0][0], _map[0][0]);
    int len = sizeof(_map)/sizeof(float)/2;
    std::cout &lt;&lt; len &lt;&lt; std::endl;
    for(int i = 1; i &lt; len; i++)
        mapPath.lineTo(QPointF(_map[i][0]*WIDTH, _map[i][1]*HEIGHT));
}

void MapScene::drawBackground(QPainter *painter, const QRectF &amp;)
{
    std::cout &lt;&lt; "draw background" &lt;&lt; std::endl;
    painter-&gt;drawPath(mapPath);
}
</code></pre>

<p>My <strong>mainwindow.cpp</strong></p>

<pre><code>#include "mainwindow.h"
#include "ui_mainwindow.h"
#include &lt;iostream&gt;
#include "mapscene.h"

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

    MapScene *scene = new MapScene();
    ui-&gt;graphicsView-&gt;setScene(scene);
    resizeEvent(0);
}

void MainWindow::resizeEvent(QResizeEvent *)
{
    QRectF bounds = ui-&gt;graphicsView-&gt;scene()-&gt;sceneRect();
    ui-&gt;graphicsView-&gt;fitInView(bounds, Qt::KeepAspectRatio);
    ui-&gt;graphicsView-&gt;centerOn(bounds.center());
    std::cout &lt;&lt; "resize - scene rect: " &lt;&lt; ui-&gt;graphicsView-&gt;sceneRect().width() &lt;&lt; " x " &lt;&lt; ui-&gt;graphicsView-&gt;sceneRect().height() &lt;&lt; std::endl;
}

MainWindow::~MainWindow()
{
    delete ui;
}
</code></pre>

<p>I am using Qt 5.5.1 (Ubuntu).</p>

<p><strong>EDIT:</strong> I have changed <code>\n</code> to <code>std::endl</code> as @LogicStuff suggested and now the message gets printed, so <code>drawBackground()</code> <strong>is</strong> getting called. The problem seems to be with <code>QGraphicsView::fitInView()</code> and <code>QGraphicsView::centerOn()</code>. I have changed the post accordingly.</p>