0

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>
Adrian Maire
  • 14,354
  • 9
  • 45
  • 85
  • 1
    What is the content of `darkstyle.qrc`? – GreenScape Mar 16 '15 at 09:22
  • I added the content of the .qrc file in the question text (EDIT 1 section) – Adrian Maire Mar 16 '15 at 09:28
  • 1
    Have you tried call Q_INIT_RESOURCE(resources) or add qrc:// in the path? http://doc.qt.io/qt-5/resources.html – dfranca Mar 16 '15 at 09:38
  • 1
    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 Mar 16 '15 at 09:39
  • @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 Mar 16 '15 at 09:56
  • @danielfranca: using this macro result in "unresolved external symbol "int __cdecl qInitResources_darkstyle(void)" The problem seem to be linked to MSVS I will downgrade to 2010, to see if that is the problem. As I just tried with G++ and it work correctly. – Adrian Maire Mar 16 '15 at 09:56

2 Answers2

2

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.

qtanswers
  • 21
  • 1
0

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.

Adrian Maire
  • 14,354
  • 9
  • 45
  • 85