PDA

View Full Version : QGraphicsView wrong initial size of objects



syrtyr
13th July 2009, 15:14
Hi!

I'm having trouble with a QGraphicsView. Basically, I want to draw some items (a circle in this example) in a way, that they always fill the complete view.

I did put the QGraphicsView on the main window using qtcreators designer and use it with the following mainwindow.cpp (full example in as attachement):



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPen>

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

circle = new QGraphicsEllipseItem(-10, -10, 20, 20);
circle->setPen(QPen(Qt::black));
scene.addItem(circle);
ui->graphicsView->setScene(&scene);
ui->graphicsView->fitInView(circle, Qt::KeepAspectRatio);
}

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

void MainWindow::resizeEvent(QResizeEvent *e) // virtual
{
ui->graphicsView->fitInView(circle, Qt::KeepAspectRatio);
QMainWindow::resizeEvent(e);
}


Now what happens is when the window opens, the items do not fill the whole QGraphicsView, they are too small:
3436
But after the first resize event the fitInView function works as expected and the items fill the whole space:
3437

Does somebody have an idea why this happens and how to avoid it?

Regards,

syrtyr

wysota
13th July 2009, 15:34
In the constructor the size of the view is not determined yet so fitInView will work on bogus data. You should call fitInView from within showEvent().

syrtyr
14th July 2009, 07:13
Thanks! That did the trick! :)