PDA

View Full Version : Qt Script add method



bunjee
7th May 2009, 01:15
Hey there,

Here is a bit advanced question,

I'm using the following routine to enable my QtScript to new a QLineEdit:


Q_SCRIPT_DECLARE_QMETAOBJECT(QLineEdit, QWidget*)

...

QScriptValue lineEditClass = engine.scriptValueFromQMetaObject<QLineEdit>();
engine.globalObject().setProperty("QLineEdit", lineEditClass);

Now I want to make one of this QLineEdit's member (not a slot, not a property) available in the script:

example: "void QLineEdit::deselect ()"

and in the script


lineEdit = new QLineEdit;
lineEdit.deselect();

What's the most simple way to achieve that.

Thanks.

wysota
7th May 2009, 01:55
Either add a function to the prototype of the QLineEdit class (in QScript) that will call deselect on the real object or subclass QLineEdit and use the Q_INVOKABLE macro.

bunjee
10th May 2009, 18:30
Thanks Wysota,

The best solution is using "setDefaultPrototype" so it applies to every inheriting objects.

Here is the implementation.

(Courtesy of Kent himself from Trolltech).


#include <QtScript>
#include <QtGui>

static QScriptValue QWidget_prototype_move(QScriptContext *ctx, QScriptEngine *eng)
{
QWidget *widget = qscriptvalue_cast<QWidget*>(ctx->thisObject());
Q_ASSERT(widget != 0);
widget->move(ctx->argument(0).toInt32(), ctx->argument(1).toInt32());
return eng->undefinedValue();
}

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QScriptEngine eng;
QScriptValue widgetProto = eng.newObject();
widgetProto.setProperty("move", eng.newFunction(QWidget_prototype_move));
eng.setDefaultPrototype(qMetaTypeId<QWidget*>(), widgetProto);

QMainWindow win;
win.resize(400, 400);
QScriptValue scriptWindow = eng.newQObject(&win);
scriptWindow.property("move").call(scriptWindow, QScriptValueList() << 100 << 200);

win.show();
return app.exec();
}

mpantoli
15th June 2013, 12:46
Hi all,
In my project, I need to espose to the script, the move slot(normaly privated) of all widgets that I use.
I've implemented a solution that use QUiLoad to load a ui created by QtDesigner and everything works well, but I need to move some widgets at run-time inside the Script.
I think that the best solution is to use the "setDefaultPrototype" when I create widgets(by QUiLoad), but since now I don't find the right way...

If someone has a peace of code as example .....

Thanks
mpantoli

wysota
15th June 2013, 13:35
I think you rather need to expose the top level widgets and access all the other widgets through the parent-child relationship. If the latter is not possible, traverse the widget tree and expose each of them separately.