2012-12-16 138 views
0

我有我的所有代码在一个mainWindow的构造函数。问题在于显示器只弹出一秒钟而消失。任何帮助都感激不尽 。以下是代码。QGraphicScene Pop一秒钟然后消失

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); 


} 

回答

1

您已在栈上创建场景并且未将其分配给成员变量,因此一旦控件离开构造函数,它将被删除。

+0

所以我创建的所有变量都应该是主窗口类的数据成员。应该是指针,其内存我在构造函数中分配,然后通过其他函数更新我的程序? –

+0

不,只有需要持久保存比构造函数更长的堆栈对象才在范围内。我建议你阅读一本好的C++书,不理解对象的一生会让生活困难。 – cmannett85

相关问题