Search code examples
qtresourcesimageqgraphicsviewscene

File path specification in .qrc and accessing the files from the c++ code


When i have this code and copy manually i.png to binary file destination

scene = new QGraphicsScene(this);
QPixmap pixm(qApp->applicationDirPath() + "/i.png");
scene->addPixmap(pixm);
ui->graphicsView->setScene(scene);

Image shows exactly as i want. Now i created resource file images.qrc. Path is set to / and contains only one file i.png. But when i change code to:

scene = new QGraphicsScene(this);
QPixmap pixm(":/images/i.png");
scene->addPixmap(pixm);
ui->graphicsView->setScene(scene);

GraphicsView remains empty. Why?

There is images.qrc

<RCC>
    <qresource prefix="/">
        <file>i.png</file>
    </qresource>
</RCC>

Solution

  • ":/images/i.png" assumes that the prefix is images. Rather use

     QPixmap pixm(":/i.png");