search

Home  >  Q&A  >  body text

c++ - qt上luaL_loadfile文件路径问题

我有个lua文件需要在qt中调用,

lua_State *lu = luaL_newstate();
int code = luaL_loadfile(lu, "parse.lua"); //文件读取不到

问题来了这个lua文件应该如何放到qt工程目录中?

以下是无效的:

  1. 试了把lua放到qrc资源中,但是":/.../.." 这种路径字符串无法被luaL_loadfile处理?

  2. qt工程add other files 加入lua文件,(.pro配置中添加distfiles属性)

  3. 手动采用源码存储的相对路径

以下是有效的:

但是我想用lua_loadfile....
我理想的是qt在build可执行文件时候把lua脚本直接放在mac的app包里(如果是在mac上编译),或者win上直接写进exe的二进制里?这个思路是对的么,还是有其他方式?

迷茫迷茫2803 days ago565

reply all(1)I'll reply

  • 阿神

    阿神2017-04-17 13:49:52

    When running in QtCreator, the running directory is the build directory, not the directory where the source code is located

        qDebug() << QDir::currentPath();

    So the lua script file should be in the build directory.
    When using resources, the file content will be directly embedded into the program, read using QFile, and then lua_loadbuffer

    reply
    0
  • Cancelreply