You need to set a scene to your QGraphicsView
first:
ui->graphicsView->setScene(new QGraphicsScene);
Right now ui->graphicsView->scene()
is returning a null pointer.
QGraphicsView::scene()
: Returns a pointer to the scene that is currently visualized in the view. If no scene is currently visualized, 0 is returned.
Also, instead of creating a QGraphicsRectItem
and using its rect()
method to return a QRect
object that you will use in QGraphicsScene::addRect()
, you should just create a QRect
object and use that:
QRect rect(10,10,10,10);
ui->graphicsView->scene()->addRect(rect);