9 окт. 2008 г.

Qt4: Create Qt object instance from qt script

There is a simple way to create QT object instances from qt script.
For instance, let's create a QDialog from qs.
  • Declares the given QMetaObject:
    Q_SCRIPT_DECLARE_QMETAOBJECT(QDialog, QWidget*)

  • Creates a QScriptValue that represents the Qt class
    QScriptValue dialogClass = engine.scriptValueFromQMetaObject<QDialog>();
    engine.globalObject().setProperty("QDialog", dialogClass);

  • code.qs is:
    var d = new QDialog();
    d.windowTitle = "qs dialod"
    d.show()

  • run scriplet from qt/c++:
    QFile file("code.qs");
    file.open(QIODevice::ReadOnly);
    engine.evaluate(file.readAll());
    file.close();



reference articles:

Комментариев нет: