0
votes

I try to include style images of my app into a q-resource file. When I include the file directly in the code, it work, but when i try to use QResource, it fail (do not load the file).

I have the resource file in the main directory:

AppFolder
  |- main.cpp
  |- darkstyle.qrc
  |- darkstyle
       |- WindowTitleBar.png

The following example print: failed1 failed2

#include <QApplication>
#include <QResource>
#include <Qfile>
#include <QDebug>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    bool ok= QResource::registerResource("darkstyle.qrc");
    if (!ok) qDebug()<<"failed1";

    QFile file(":/darkstyle/WindowTitleBar.png");
    //QFile file("../AppFolder/darkstyle/WindowTitleBar.png"); //that work

    if(!file.open(QFile::ReadOnly | QFile::Text)) qDebug()<<"failed2";
    else file.close();

    //return a.exec();
    return 0;
}

Note: Qt creator by default create binaries (.exe) in a top folder: ../build-AppFolder_Qt_5_4_1_MSVC2013_64bit-Debug/debug/AppFolder.exe The execution root path seem to be: ../build-AppFolder_Qt_5_4_1_MSVC2013_64bit-Debug

I tried most of possible combinations with execution paths.

Note2: Some examples use a .rcc file format, I have none of these, but that could be a clue.

Summary: How to access a QResource file from inside a QT app?

EDIT 1: Content of qrc file:

<RCC>
    <qresource prefix="/">
        <file>darkstyle/WindowTitleBar.png</file>
        <file>darkstyle/WindowTitleButton.png</file>
        <file>darkstyle/WindowTitleButton1.png</file>
        <file>darkstyle/WindowTitleButton2.png</file>
        <file>darkstyle/WindowTitleButton3.png</file>
    </qresource>
</RCC>
2
What is the content of darkstyle.qrc? - GreenScape
I added the content of the .qrc file in the question text (EDIT 1 section) - Adrian Maire
Have you tried call Q_INIT_RESOURCE(resources) or add qrc:// in the path? doc.qt.io/qt-5/resources.html - dfranca
If you uncomment your working solution and alter a bit to QFile file("darkstyle/WindowTitleBar.png"); will it work? It seems to me that the problem is not with resource file but with a working directory. - GreenScape
@GreenScape: no, as I said, the working directory is ../build.... But i tried to set the .qrc file with a absolute/relative correct path to all: the project folder, the exec folder and the top folder. - Adrian Maire

2 Answers

2
votes

QResource::registerResource("darkstyle.qrc") registers the resource description. If you want to use resources dynamically like this you need to register the compiled resources themselves. Run rcc -binary darkstyle.qrc -o darkstyle.rcc and use QResource::registerResource("darkstyle.rcc")

Alternatively, compile the resources into your binary directly. Do do so, use RESOURCES += darkstyle.qrc in your .qrc, and leave out the QResource::registerResource.

0
votes

The problem is related to an incompatibility of the given version of QT with MSVS2013. The problem is solved by downloading another version of QT or visual studio.