.qrc中的文件路径规范以及从c ++代码访问文件

时间:2013-05-02 08:59:09

标签: qt resources image qgraphicsview scene

当我有这个代码并手动将i.png复制到二进制文件目的地

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

图像完全符合我的要求。现在我创建了资源文件images.qrc。路径设置为/并且只包含一个文件i.png。但是,当我将代码更改为:

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

GraphicsView保持为空。为什么呢?

有images.qrc

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

2 个答案:

答案 0 :(得分:4)

":/images/i.png"假设前缀为images。而是使用

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

答案 1 :(得分:2)

为了指定:/images/i.png的位置,我希望资源文件看起来更像这样: -

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