[Qt-interest] QScriptValue::setPrototype doens't work in Qt4.6?

Dong Tiger idlecat511 at gmail.com
Mon Dec 28 06:30:38 CET 2009


Hi,

My code that works with Qt4.5 doesn't work with 4.6. I wrote a short code to
demonstrate the problem. What I want to do is trying
to get ResolveClass::property being called when a global variable b is
referred.

#include <QCoreApplication>
#include <QScriptEngine>
#include <QScriptClass>
#include <QtDebug>

class ResolveClass : public QScriptClass {
 public:
  ResolveClass(QScriptEngine *engine) : QScriptClass(engine) {}
  QScriptValue property(const QScriptValue &obj, const QScriptString &name,
uint id) {
    qDebug() << name;
  }
};

int main(int argc, char **argv) {
  QCoreApplication app(argc, argv);
  QScriptEngine engine;
  ResolveClass resolve_class(&engine);

  // method1: doesn't work.
  // engine.globalObject().setScriptClass(&resolve_class);

  // method2: works with 4.5 but not with 4.6.
  // engine.globalObject().setPrototype(engine.newObject(&resolve_class));

  // method3: doesn't work.
  // engine.setGlobalObject(engine.newObject(&resolve_class));

  // method4: doesn't work.
  // QScriptValue global = engine.newObject();
  // global.setPrototype(engine.newObject(&resolve_class));
  // engine.setGlobalObject(global);

  // This works but not what I need.
  QScriptValue global = engine.newObject();
  global.setProperty("b", 200);
  engine.setGlobalObject(global);

  engine.evaluate("var a = 100; a = b;");
  QScriptValue a = engine.globalObject().property("a");
  qDebug() << a.toInt32() << "\n";
  return 0;
}

I don't have Qt4.5 at hand so I didn't verify if these methods work with
Qt4.5 except method2.
Any  idea of that? Thanks in advance.

--
Tiger
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091228/c0fe7a63/attachment.html 


More information about the Qt-interest-old mailing list