我得到以下错误
"TypeError: cannot call sayHello(): argument 2 has unknown type `QString&' (register the type with qScriptRegisterMetaType())"
当执行我的 Qt 程序 test.exe 来调用 javascrip 文件 haha.js。我想做的是从“void sayHello(const QString &name, QString &result);”返回一个值(“result”)在 JavaScript 中。看起来 QtScript 理解 (const QString &) 但无法理解 (QString &)。知道我做错了什么吗?
哈哈.js
h = new Haha();
result = "";
h.sayHello("henry", result);
result;
Qt 程序包括:haha.h、haha.cpp、main.cpp
哈哈
#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
哈哈.cpp
#include "haha.h"
Haha::Haha(QObject *parent) :
QObject(parent)
{
}
void Haha::sayHello(const QString &name, QString &result)
{
result = "Hello " + name;
}
主文件
#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();
}