Есть ещё одно решение.
static QScriptValue includeScript ( QScriptContext *context, QScriptEngine *engine )
{
QScriptValue ret ( false );
if ( engine == 0 )
return ret;
QString fileName = context->argument ( 0 ).toString();
QFile file ( fileName );
if ( file.open ( QFile::ReadOnly ) )
{
QByteArray line = file.readAll();
file.close();
QString contents = QString::fromUtf8 ( line );
// ударим в бубны !
engine->currentContext()->setActivationObject ( engine->currentContext()->parentContext()->activationObject() );
ret = engine->evaluate ( contents, fileName );
}
return ret;
};
После чего при инициализации engine:
...
_engine->globalObject().setProperty ( "include", _engine->newFunction ( includeScript ) );
...
Записываем скрипт "ac.qs", для примера взят код AnalogClock.
function AnalogClock(parent) {
QWidget.call(this, parent);
var timer = new QTimer(this);
timer.timeout.connect(this, "update()");
timer.start(50);
this.setWindowTitle("Analog Clock");
this.resize(400, 400);
}
AnalogClock.prototype = new QWidget();
AnalogClock.prototype.paintEvent = function() {
var side = Math.min(this.width, this.height);
var time = new Date();
var painter = new QPainter();
painter.begin(this);
painter.setRenderHint(QPainter.Antialiasing);
painter.translate(this.width / 2, this.height / 2);
painter.scale(side / 200.0, side / 200.0);
painter.setPen(new QPen(Qt.NoPen));
painter.setBrush(new QBrush(AnalogClock.hourColor));
painter.save();
painter.rotate(30.0 * (time.getHours() + time.getMinutes() / 60.0));
painter.drawConvexPolygon(AnalogClock.hourHand);
painter.drawLine(0, 0, 100, 100);
painter.restore();
painter.setPen(AnalogClock.hourColor);
for (var i = 0; i < 12; ++i) {
painter.drawLine(88, 0, 96, 0);
painter.rotate(30.0);
}
painter.setPen(new QPen(Qt.NoPen));
painter.setBrush(new QBrush(AnalogClock.minuteColor));
painter.save();
painter.rotate(6.0 * ( time.getMinutes() + time.getSeconds() / 60.0 + time.getMilliseconds() / 60000.0 ) );
painter.drawConvexPolygon(AnalogClock.minuteHand);
painter.restore();
painter.setPen(AnalogClock.secondColor);
painter.setBrush(new QBrush(AnalogClock.secondColor));
painter.save();
painter.rotate(6.0 * (time.getSeconds() + time.getMilliseconds() / 1000.0));
painter.drawConvexPolygon(AnalogClock.secondHand);
painter.restore();
painter.setPen(AnalogClock.minuteColor);
for (var j = 0; j < 60; ++j) {
if ((j % 5) != 0)
painter.drawLine(92, 0, 96, 0);
painter.rotate(6.0);
}
painter.end();
};
AnalogClock.hourColor = new QColor(127, 0, 127);
AnalogClock.minuteColor = new QColor(0, 127, 127, 191);
AnalogClock.secondColor = new QColor(0, 127, 127, 32);
AnalogClock.hourHand = new QPolygon([new QPoint(7, 8),
new QPoint(-7, 8),
new QPoint(0, -40)]);
AnalogClock.minuteHand = new QPolygon([new QPoint(4, 10),
new QPoint(-4, 10),
new QPoint(0, -70)]);
AnalogClock.secondHand = new QPolygon([new QPoint(1, 20),
new QPoint(-1, 20),
new QPoint(0, -97)]);
И собственно вызов в коде qs:
include("./ac.qs");
// вызов функций и создание объектов описанных в скрипте
var clock = new AnalogClock();
clock.show();
QCoreApplication.exec();
В скриптовом отладчике можно будет видеть в каком файле скрипта есть ошибки, и появится возможность расставить брейкпойнты.