Qt with Lua | Where to place lua file(s)

独自空忆成欢 提交于 2019-12-11 06:48:47

问题


I've created a cpp file with the main method implementing calls to lua. When compiling + executing the qt project I receive the following error:

PANIC: unprotected error in call to Lua API (attempt to call a string value)

The problem is that lua cannot find the lua file to be executed (at least I think it is). So I copied the file to all the debug dirs and the main dir but it still didn't work.

Thanks for helping me!


main.cpp

#include <stdio.h>

extern "C" {
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
}

lua_State* L;

int main(int argc, char *argv[])
{


    /* initialize Lua */
    L = luaL_newstate();

    /* load Lua base libraries */
    luaL_openlibs(L);

    /* load the script */
    luaL_loadfile(L, "test.lua");

    lua_call(L, 0, 0);

    /* cleanup Lua */
    lua_close(L);

}

and the file test.lua

-- test

print("Hello World")

回答1:


Given that this is a run-time file operation, the function is likely only looking in the current directory.




回答2:


It seems like Qt uses the user's directory as default path.

You can validate this with: (thanks Rich)

QDir dir(".");
qDebug() << dir.absolutePath();

When putting the lua script into this folder everything works like a charm.

The working directory can be set with the following command:

QDir::setCurrent()

And in combination with

QCoreApplication::applicationFilePath() 

The path can be set to where the exe resigns in.




回答3:


This is not really related to Qt since you are not using any of the Qt API in your software.

You should check the status returned by the lual_loadfile method like shown in this example. That should give you some additional clues about what is going wrong.

Just in case, there's a QtLua module that could be of interest.



来源:https://stackoverflow.com/questions/41667309/qt-with-lua-where-to-place-lua-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!