将成员函数与 QScriptEngine::newFunction 一起使用
让我们以一个简单的类为例:
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);
....
}
上面的函数给了我一个错误: 没有匹配的函数来调用 'QScriptEngine::newFunction(
我尝试过使用 engine.newFunction(reinterpret_cast
但这给了我一个错误,基本上表明编译器不知道名为 'FunctionSignature'
的关键字。
任何帮助表示赞赏。多谢。
问候, 罗汉
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看官方示例代码,您应该执行以下操作:
编辑:好的,我进一步研究了参考文献。问题是,您尝试传递需要函数的方法。正如 @mosg 指出的,这是不可能的。要么将 foo 设为静态函数,要么在引用的线程中尝试解决方案。这意味着,您可以通过
engine.newQObject
创建一个新的QObject
。Looking into the official example code, you should do something like this:
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 newQObject
viaengine.newQObject
.解决静态方法:
Solution static method: