Using a member function with QScriptEngine::newFunction

我与影子孤独终老i 提交于 2020-01-02 10:04:13

问题


Let's take the case of a simple class:

QScriptEngine engine;

class MyClass {
public:
    QScriptValue foo(QScriptContext*, QScriptEngine*);
    MyClass();
};

QScriptValue MyClass:foo(QScriptContext* context, QScriptEngine* eng) {
    //something
}

MyClass::MyClass() {
    QScriptValue self = engine.newFunction(this->foo, 0);
    ....
}

The above function gives me an error: no matching function for call to ‘QScriptEngine::newFunction(<unresolved overloaded function type>, int)’

I have tried using engine.newFunction(reinterpret_cast<FunctionSignature>(foo), 0); but this gives me an error which basically says that the compiler is not aware of a keyword called 'FunctionSignature'.

Any help is appreciated. Thanks a lot.

Regards, rohan


回答1:


Looking into the official example code, you should do something like this:

MyClass::MyClass() {
    QScriptValue self = engine.newFunction(foo, 0);
    ....
}

EDIT: OK, I looked into the reference a little bit more. The problem is, that you try to pass a method, where a function is needed. As @mosg pointed out, this isn't possible. Either you make foo a static function - or you try the solution in the referenced thread. This means, that you create a new QObject via engine.newQObject.




回答2:


Solution static method:

class MyClass {
public:
    static QScriptValue foo(QScriptContext*, QScriptEngine*);
    MyClass();
};

...

QScriptValue func = engine.newFunction(MyClass::foo)  


来源:https://stackoverflow.com/questions/2935802/using-a-member-function-with-qscriptenginenewfunction

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!