[Interest] QtScript can't call function with argument type “QString &”

ZHONG Zhu Zhu.Zhong at alcatel-sbell.com.cn
Tue Jul 2 07:03:31 CEST 2013


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();
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130702/e0883dc4/attachment.html>


More information about the Interest mailing list