[Qt-interest] Cross-thread Scripting & GUI interaction

stewart at stewartholmes.com stewart at stewartholmes.com
Tue Oct 13 11:16:31 CEST 2009


Hi all,

I'm building an application using QtScript. All my calls to ScriptEngine.eval() are in a separate thread.

I want to create a static function, so I try this:

ScriptEngine engine;
QScriptValue fun = engine->newFunction(test);
engine->globalObject().setProperty("test", fun);

I then define test as:

QScriptValue test(QScriptContext * context, QScriptEngine * engine)
{
    return QString("blah blah");
}

All is fine here. However, what I want to do, is have the function test show a file open dialog, and then return the result of this. I can't call any GUI functions from out of the event thread. Normally I'd handle this by passing my main window as callee data to the test function. I could then do:

QScriptValue test(QScriptContext * context, QScriptEngine * engine)
{
    QScriptValue calleeData = context->callee().data();
    MainWindow *main= qobject_cast<MainWindow*>(calleeData.toQObject());
    main->foo();
}

where foo is defined as:

void foo() { emit fooSignal(); }

then I make sure that fooSignal is connected to a method that performs my GUI functions. This way I am using cross-thread signals to safely post onto the event thread. However, I can't do that in my case, because I need to wait for the result of the file dialog. Any ideas if I can do this?

Thanks,
Stewart





More information about the Qt-interest-old mailing list