Qt World Summit: Register Today!

[solved] script not re-evaluating

  • Hi,

    I have a quite large qt app and want to avoid re-compiling every time i want to change a configuration parameter.
    I thought of adopting a javascript configuration file to be loaded at runtime (using QScriptEngine), everything works perfectly, except if i change the content of the javascript file and re-run the app (without recompiling) the contents of the javascript file are still what they were in the previous run.
    The only way i have to refresh the contents is to do a clean all / run, which defeats the purpose (because i'm back to square 1 at recompiling).

    I tried pushing and popping contexts in QScriptEngine to no avail, and I tried googling as much as possible about scripts not reloading but it seems like there's very little and not entirely relevant.
    I'd be extremely grateful if anybody can help out.
    This is what I'm doing:
    QScriptEngine engine;
    QString configFilename(":/config.qs");
    QFile configScript(configFilename);
    QTextStream stream(&configScript);
    QString contents(stream.readAll());

    Config appConfig;
    appConfig.debug = false;
    appConfig.appname = "test";
    QScriptValue cnf = engine.newQObject(&appConfig);
    QScriptContext * ctx = engine.pushContext();
    ctx->activationObject().setProperty("config", cnf);
    engine.evaluate(contents, configFilename);
    qDebug() << "cnf: " << cnf.property("debug").toBoolean() << ", appname: " << cnf.property("appname").toString();

  • For the record, I resolved the problem.
    The problem was that the config file was loaded as a resource (using the ":" accessor). Obviously this causes qt to "bake" the resource and not reload its contents, which makes sense.
    Using an absolute path resolved the problem.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Glad you found out and thanks for sharing your findings.

    Can you also please update the thread title prepending [solved] so other forum users may know a solution has been found :)

    Happy coding !

Log in to reply