doomguy
doomguy

Reputation: 419

QGraphicsView does not show anything in Qt 5.0

I am new to Qt 5.0 and I am trying to use a QGraphicsView called "missionView" of size 700x400 inside a QWidget to show a rectangle. However, nothing shows up inside the graphics view on running the application. Here is the constructor of the QWidget "MainView" where I am doing everything

MainView::MainView(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainView)
{
ui->setupUi(this);
QGraphicsScene scene(0, 0, 500, 500);
QGraphicsRectItem* myrect = scene.addRect(QRectF(0,0,15,5),QPen(), QBrush());


ui->missionView->setScene(&scene);

ui->missionView->setVisible(true);
ui->missionView->show();
ui->missionView->update();
printf("QGraphicsScene scene's items: %d\n",scene.items().size());
    for (int i = 0; i < scene.items().size(); i++) {
        printf("%d\n",scene.items().at(i));
    }


}

The last print statement does show that one item has been added but still nothing gets shown. I have tried an approach similar to this but this too dosent work. Can anyone please explain this.

Upvotes: 0

Views: 1107

Answers (1)

Frank Osterfeld
Frank Osterfeld

Reputation: 25155

You're creating the scene object on the stack. Thus the scene will be destroyed right away at the end of the constructor and thus nothing will be shown. Create the scene on the heap and/or make it a class member and it should work.

Upvotes: 1

Related Questions