Search code examples
c++qtwebassemblyqmakeemscripten

Using preload-file compiler option for emscripten compiler to load local file in Qt


I am building an Qt Application using WebAssembly. I want to open a local file and read it frin my code which isn't exacty easy with WebAssembly. I tried using the preload-file option (reference) in my .pro file:

CONFIG += preload-file /path/to/file/Settings.ini

However, when compiling my application with WebAssembly, It fails showing my the following error:

Application exit (RuntimeError: Aborted(native code called abort()))

I am currently using Qt 6 with the fitting Emscripten version. My code works when compiling with GCC. Of course, emscripten compiles when removing the file handling stuff from my code. Using a file dialog isn't a suitable option.


Solution

  • I noticed you are using CONFIG += but the emscripten reference says it is a linker flag. Try using instead QMAKE_LFLAGS += --preload-file /path/to/file/Settings.ini.

    Personally, I have not tested preload-file option but two other options have been working for me:

    1. The similar embed-file functionality. In the .pro file add QMAKE_LFLAGS += --embed-file ../../cdpcore/Templates/Models@Models to make the folder specified to be available in [working directory]/Models.
    2. Use the Qt Resource system to embed files. Basically add a .qrc file to your project that lists resources to embed into the binary and then into the .pro file add RESOURCES += resources.qrc.