QGraphicsScene,QGraphicsView和QVBoxLayout()问题

大家问候, 每当我运行它时,Qt Creator就会停止工作,当我注释掉线路时
//mainWindow = new MainWindow();
但是如果没有注释掉线路,那么将会出现很多次。另外要检查的是,
QGraphicsScene
QGraphicsView
QVBoxLayout
的结构是否正确,因为在main.cpp中调用MainWindow时似乎无法显示视图。 谢谢 :)
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), keydown_left(false), keydown_right(false),scene(NULL), view(NULL)
{
    qDebug("MainWindow");
    //mainWindow = new MainWindow();
    //qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    scene = new QGraphicsScene(mainWindow);
    view = new QGraphicsView(scene, mainWindow);

    scene->setSceneRect(-300, 0, 640, 360);
    scene->setItemIndexMethod(QGraphicsScene::NoIndex);
    Egg *egg = new Egg();

    QVBoxLayout *layout = new QVBoxLayout();

    for (int i = 0; i < ObjCount; ++i) {
        FlyingObj *Fo= new FlyingObj();
        Fo->setPos(::sin((i * 6.28) / ObjCount) * 500,
                   ::cos((i * 6.28) / ObjCount) * 500);
        scene->addItem(Fo);
    }
    egg->setPos((640-64)/2, 100);//16 - 2*16
    scene->addItem(egg);


    view->setRenderHint(QPainter::Antialiasing);
    view->setBackgroundBrush(QPixmap(":/bg.jpg"));
    //view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    view->setDragMode(QGraphicsView::NoDrag);
    view->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Egg game"));
    view->resize(640, 360);
    QObject::connect(scene, SIGNAL(keyPressEvent(QKeyEvent)), this, SLOT(keyPressEvent(QKeyEvent)));
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setFrameStyle(QFrame::NoFrame);
    view->setFocusPolicy(Qt::NoFocus);
    view->setCacheMode(QGraphicsView::CacheBackground);
    view->showFullScreen();

    layout->addWidget(view);
    QWidget *w = new QWidget();
    w->setLayout(layout);
}
    
已邀请:
尝试在项目中包含main函数,通常在文件中单独定义:Main.cpp,在其中实例化MainWindow类:
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
在MainWindow构造函数中取消注释mainWindow是递归调用,在此上下文中是不正确的方式。最后,尝试改变:
    layout->addWidget(view);
    QWidget *w = new QWidget();
    w->setLayout(layout);
成:
   setLayout(layout);
    

要回复问题请先登录注册