Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Files from Assetsfolder in C++



  • Hi All,
    I think I don't get the point with the assets folder. I want to deploy some files with the assets folder (not in a qrc).
    Within qml I can perfectly access them with:

    Qt.resolvedUrl("../../assets/" + fileName)
    

    But I can't get them within C++:

    QFile file("assets:/" + fileName);
    

    From what I have read the "assets:/" works like the "file:/" prefix but I always get fileExists false in C++. But its there because from qml I can access it.....

    Any hints?
    Best,
    Patrik



  • @patrikd
    What platform are you on? I read that QFile("assets:/") is Android only, but you don't say.

    Otherwise you may need to state your Qt version, e.g. https://stackoverflow.com/questions/48211797/copy-file-from-assets-to-permanent-location-qt-android.



  • I'm currently trying it on Android and Win-Desktop. Both not working. Qt 5.13.2.


  • Moderators

    @patrikd
    just to clarify it wont work on Windows, only on Android (device and emulator).
    Any chance you simply incorrectly assume that those 2 paths point to the same file?!
    Simply traverse the "assets:/" root folder and check it's contents.



  • @raven-worx
    Hi Raven,

    so if I make:

        QDir dir("assets:"); //with / and without /
        QFileInfoList list = dir.entryInfoList();
        for (int i = 0; i < list.size(); ++i) {
            QFileInfo fileInfo = list.at(i);
            qDebug() << fileInfo.fileName();
        }
    

    Nothing is printed out.
    Is there a C++ equivalent for:

    Qt.resolvedUrl("../../assets/" + fileName)
    

    ? Because this one works like charm on Android, iOS, Windows and MacOs.
    In my .pro file I have:

    assetsFolder.source = assets
    DEPLOYMENTFOLDERS += assetsFolder
    

    Thx,
    Patrik


  • Moderators

    @patrikd said in Files from Assetsfolder in C++:

    Because this one works like charm on Android, iOS, Windows and MacOs.

    What?!
    assets: is just for accessing Android's assets resources.

    Qt.resolvedUrl() loads relative urls from it's loading url context.
    I don't know your exact desired folder structure.
    Are you actually trying to read files from the filesystem?
    How are you loading your inital QML root file actually?



  • @raven-worx
    Entry point:

        felgo.setMainQmlFileName(QStringLiteral("qml/Main.qml"));
        engine.load(QUrl(felgo.mainQmlFileName()));
    

    With the setting in the pro file I get the asset folder deployed on all plattforms. So the folder is in a way accesible. But
    I don't get it in C++.

    Here the folders:
    folders.PNG
    Thx,
    Patrik


Log in to reply