Search code examples
c++qtqgraphicsview

Adding an image to QtGraphicsView?


Hi iv looked at other questions and solutions for this but none seem to help for my specific problem.

Im simply trying to add a picture to my GraphicsView that i have added using Qt designer

.cpp

void test::populateScene()
{
     QImage image(":/images/myFile.png");
     QGraphicsPixmapItem item(QPixmap::fromImage(image));
     QGraphicsScene *scene = new QGraphicsScene;
     scene->addItem(&item);
     ui->graphicsView->setScene(scene); 
}

i have the necessaary includes but when i click run the program just puts up a not responding message straight away

i have no compiler errors just hit run and then then get test.exe is not responding

any ideas, this seems like it should be really simple but i cant work out why this isnt right (mainly due to no compiler errors to look through and find the cause of the crash)


Solution

  • [SOLVED]

        QImage image(":/images/myFile.png");
        QGraphicsScene *scene = new QGraphicsScene();
        scene->addPixmap(QPixmap::fromImage(image));
        scene->setSceneRect(0,0,image.width(),image.height());
        ui->graphicsView->setScene(scene);
    

    No other solution i found here or elsewhere for getting an image into a graphicsview have worked so i will post this to help others now my problem is solved feel free to ask questions