PDA

View Full Version : QGraphicsScene segmentation fault



maxpower
20th November 2006, 18:17
I have the following code


#include <QGraphicsScene>
#include <QMessageBox>
#include "proFootballOnline.h"
//
ProFootballOnline::ProFootballOnline( QWidget * parent, Qt::WFlags f)
: QMainWindow(parent, f)
{
setupUi(this);
}

void ProFootballOnline::Init ()
{

}
void ProFootballOnline::on_initPB_clicked()
{
QGraphicsScene scene;
scene.addText("Bob");
//scene.setItemIndexMethod(QGraphicsScene::NoIndex);
//fieldGV->setBackgroundBrush(Qt::green);
fieldGV->setScene(&scene);
fieldGV->update();
}


When the program runs and the initPB is clicked, the program closes a does a core dump. I ran it through a debugger and, depending on what I try to do with the scene, faults on various things (setBackground, setItemIndexMode). The fieldGV is a QGraphicsView I have on my main window. Any ideas on what is causing the crash? If I place a QMessageBox at the end of the slot, i can see the contents of fieldGV update properly but closing the box sets off the core dump.

mAx

e8johan
20th November 2006, 18:23
Your problem is that scene is allocated on the stack, e.g. you do not write ... *scene = new... This means that scene is automatically deleted as soon as you leave the slot.

maxpower
20th November 2006, 18:28
Yep, yep...stupid stupid stupid.

Thanks
mAx