I got below error "TypeError: cannot call sayHello(): argument 2 has unknown type `QString&' (register the type with qScriptRegisterMetaType())" when execute my Qt program test.exe to call a javascrip file haha.js. What I was trying to do is to return a value ("result") from "void sayHello(const QString &name, QString &result);" in javascript. Looks like QtScript understands (const QString &) but can't understand (QString &). Any idea what I did wrong? haha.js h = new Haha(); result = ""; h.sayHello("henry", result); result; the Qt program consists: haha.h, haha.cpp, main.cpp haha.h #ifndef HAHA_H #define HAHA_H
#include <QObject> class Haha : public QObject { Q_OBJECT public: explicit Haha(QObject *parent = 0); public slots: void sayHello(const QString &name, QString &result); }; #endif // HAHA_H haha.cpp #include "haha.h" Haha::Haha(QObject *parent) : QObject(parent) { } void Haha::sayHello(const QString &name, QString &result) { result = "Hello " + name; } main.cpp #include <QtCore/QCoreApplication> #include <QtDebug> #include <QtScript> #include "haha.h" Q_SCRIPT_DECLARE_QMETAOBJECT(Haha, QObject*) int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QScriptEngine engine; QScriptValue HahaClass = engine.scriptValueFromQMetaObject<Haha>(); engine.globalObject().setProperty("Haha", HahaClass); QString fileName = "haha.js"; QFile scriptFile(fileName); if (!scriptFile.open(QIODevice::ReadOnly)) { return -1; } QTextStream b(&scriptFile); QString contents = b.readAll(); scriptFile.close(); QScriptValue result = engine.evaluate(contents, fileName); qDebug()<<result.toString(); return a.exec(); }
_______________________________________________ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest