Important: Please read the Qt Code of Conduct -

can't open file from qresources

  • I want to access two text files, encoded in utf-8. I do so with the following four lines:

    QResource r1 (":/models/cube.obj"); // utf-8, 336 bytes
    QResource r2 (":/models/teapot.obj"); // utf-8, 844000 bytes

    const char* pm1 = (const char *);
    const char* pm2 = (const char *);

    The first pointer, pm1, points to valid data. Hence, I can read out my file via an std::istream -- success!
    The second pointer, pm2, points to INvalid data. I cannot read out my file this way -- why?

    What I CAN do, is to open resource ":/models/teapot.obj" via QTextStream. Unfortunately, due to the usage of a non-Qt library, I cannot use it.

    My resource file, models.qrc, looks like
    <qresource prefix="/">

    The file is listed in the .pro file with RESOURCES += models.qrc. In QT Creator, all resources which I try to access are visible. I can double click them and watch their content in the built in editor.

    I cannot figure out why one file can be accessed and the other(s) can't (A third file, minicooper.obj, cannot be accessed either.

    The problem may be similar to this post. That post is marked as "solved". However, I do not understand the solution.

  • Lifetime Qt Champion


    AFAICT, the solution is to use QFile to open and read the data from the files.

  • @SGaist Thanks. What astonisches me is, with some files, the approach via QResource works. With other, it doesn't. I still do not know why I get different results on different files.

  • Lifetime Qt Champion

    Just a wild guess but the obj file are probably loaded through assimp which has absolutely no idea of how to access a Qt resource.

  • @SGaist I process the obj files with tiny obj loader. However, loading a resource in qt and processing an obj are different things of course. First, I have to load the resource. In case of success, I process it.

    One further thing I investigated is (Unix vs. Windows) encoding. I develop using Linux, so this might be an issue. Anyway, results do not change after usage of 'dos2unix'.

  • Lifetime Qt Champion

    One thing you can to is copy the .obj file from the resources to a temporary directory and try to load it from there through tiny obj loader

  • Yes, I could do that. I do cross development so I prefer to have all resources in one single file. Otherwise I do have to care for much more than to copy my executable.

  • Lifetime Qt Champion

    Sorry, I meant that you can do the copy from within your application. Something like:

    QFile qrcObj(":/models/teapot.obj");
    QTemporaryFile file;
    std::vector<shape_t> shapes;
    std::vector<material_t> materials;
    if ( && {
        QByteArray path = QDir::toNativeSeparators(file.fileName()).toLatin1();
        std::string ret = tinyobj::LoadObj(shapes, material, path.constData);
        // the rest of your code

Log in to reply