I have a portion of code that I tested and works, and now I would like to make it a class in order to have something cleaner. The following code creates a scene containing a rectangle and displays it on the widget 'graphicview'
QGraphicsScene *scene = new QGraphicsScene;
QGraphicsRectItem *rect = new QGraphicsRectItem();
rect->setRect(0,0,100,100);
scene->addItem(rect);
ui->graphicsView->setScene(scene);
What I would like now is to create a class that contains this scene, so that I just have to call :
MyClass *myscene = new MyClass;
ui->graphicsView->setScene(myscene->scene)
The quetion is, in the class MyClass, shall I have a private argument declared as QGraphicsScene *scene = new QGraphicsScene;
, or just a private argument QGraphicsScene *scene
and then within the constructor *scene=new QGraphicsScene
And the same for where I should put the delete, within the destructor of MyScene?
edit : Based on answer, I tried to rework my code without new
:
QGraphicsScene scene;
QGraphicsRectItem rect;
rect.setRect(0,0,100,100);
scene.addItem(&rect);
ui->graphicsView->setScene(&scene);
But this code doesn't work (widget 'graphicView' displays nothing), and all the example I found of Qt use the new operator. What I am missing ?