0

I have a all my code inside the constructor of a mainWindow. The problem is that the display only pop ups for a second and than vanishes. Any help will be much appreciated . Following is the code.

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QPixmap kineticPix(":/images/kinetic.png");
QPixmap bgPix(":/images/Time-For-Lunch-2.jpg");

QGraphicsScene scene(-350, -350, 700, 700);
QGraphicsItem *buttonParent = new QGraphicsRectItem;

Button *ellipseButton = new Button(QPixmap(":/images/ellipse.png"), buttonParent);
Button *figure8Button = new Button(QPixmap(":/images/figure8.png"), buttonParent);
Button *randomButton = new Button(QPixmap(":/images/random.png"), buttonParent);
Button *tiledButton = new Button(QPixmap(":/images/tile.png"), buttonParent);
Button *centeredButton = new Button(QPixmap(":/images/centered.png"), buttonParent);

ellipseButton->setPos(-100, -100);
figure8Button->setPos(100, -100);
randomButton->setPos(0, 0);
tiledButton->setPos(-100, 100);
centeredButton->setPos(100, 100);

scene.addItem(buttonParent);
buttonParent->scale(0.75, 0.75);
buttonParent->setPos(200, 200);
buttonParent->setZValue(65);


}
jaywalker
  • 1,116
  • 4
  • 26
  • 44

1 Answers1

1

You have created the scene on the stack and not assigned it to a member variable, so as soon as control leaves the constructor it is deleted.

cmannett85
  • 21,725
  • 8
  • 76
  • 119
  • So all the variables that I have created should be data members of the main window class. The should be pointers whose memory i allocate in the constructor and then make updates to my program though some other function ? – jaywalker Dec 17 '12 at 14:09
  • No, only stack objects that need to be persistent for longer than the constructor is in scope for. I suggest you read a good C++ book, not understanding object lifetime will make life difficult. – cmannett85 Dec 17 '12 at 14:20