C++ 如何使用c++;QtScript中的std复数
我试图了解如何在QtScript中使用复数,以便可以从QtScript调用使用复数参数定义的插槽。此外,用户还可以从脚本访问复数的基本代数(+、-、exp…) 我想打电话来说明一下:C++ 如何使用c++;QtScript中的std复数,c++,qt4,scripting,complex-numbers,qtscript,C++,Qt4,Scripting,Complex Numbers,Qtscript,我试图了解如何在QtScript中使用复数,以便可以从QtScript调用使用复数参数定义的插槽。此外,用户还可以从脚本访问复数的基本代数(+、-、exp…) 我想打电话来说明一下: #include<complex> typedef complex<double> Complex; class MyCppClass : public QObject { Q_OBJECT public: ... public slots: void mySignal(Complex
#include<complex>
typedef complex<double> Complex;
class MyCppClass : public QObject
{
Q_OBJECT
public:
...
public slots:
void mySignal(Complex rCValue); !! <<== should be callable from QtScript
...
}
#包括
typedef复合物;
类MyCppClass:公共QObject
{
Q_对象
公众:
...
公众时段:
void mySignal(复数rCValue);!!我认为您必须在QtScript中实现复数代数(类似)然后修改mySignal以接受这样的对象。这不是最终答案…因为如上所述,运算符+、-和*不能用于javascript端的复杂数量。但是对于感兴趣的人,我想分享以下代码片段,它们允许使用复杂参数触发插槽
测试h:
#include <QtCore>
#include <QtScript>
#include <complex>
#include <iostream>
using namespace std;
typedef complex<double> Complex;
Q_DECLARE_METATYPE(Complex)
class TestClass : public QObject
{
Q_OBJECT
public:
TestClass() : QObject() {};
public slots:
void TestOutput(Complex rValue);
};
#包括
#包括
#包括
#包括
使用名称空间std;
typedef复合物;
Q_DECLARE_元类型(复杂)
类TestClass:公共QObject
{
Q_对象
公众:
TestClass():QObject(){};
公众时段:
void TestOutput(复数右值);
};
test.cpp:
#include "test.h"
void TestClass::TestOutput(Complex rValue)
{
cout << "received value "<< rValue << endl;
}
#包括“test.h”
void TestClass::TestOutput(复杂右值)
{
你真的不能拼写include
?:)好吧,也许这就是正确的方法。但是,据我所知,在ECMA-262又名Javascript中没有运算符重载这样的事情。我希望使用QScript继承原型包装器可以克服这一问题……但似乎我运气不好(没有+,-,*只是加,减,乘).有人能证实这一点吗?是的,我没有考虑过…遗憾的是,缺少操作重载似乎在其他宏语言中很常见。也许你的问题很容易就可以编写自己的解释器了?
#include "test.h"
QScriptValue toScriptValue(QScriptEngine *eng, const Complex& rValue)
{
QScriptValue obj = eng->newObject();
obj.setProperty("re",real(rValue));
obj.setProperty("im",imag(rValue));
return obj;
}
void fromScriptValue(const QScriptValue &obj, Complex& rValue)
{
double re=obj.property("re").toNumber();
double im=obj.property("im").toNumber();
rValue=Complex(re,im);
}
QScriptValue constructComplex(QScriptContext *context, QScriptEngine *engine)
{
Complex complex=Complex(2,1);
return engine->toScriptValue(complex);
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QScriptEngine eng;
// register our custom type
qScriptRegisterMetaType<Complex>(&eng, toScriptValue, fromScriptValue);
TestClass *test=new TestClass;
QObject *someObject = (QObject*)test;
QScriptValue objectValue = eng.newQObject(someObject);
eng.globalObject().setProperty("myObject", objectValue);
QScriptValue val = eng.evaluate("function Complex(real, imaginary) { this.re = real; this.im = imaginary;}; cadd = function (a, b) {return new Complex(a.re + b.re, a.im + b.im);};");
val = eng.evaluate("my_complex=new Complex(8,1); my_comp=new Complex(2,9); my_c=cadd(my_comp,my_complex);");
cout << "script"<< val.toString().toStdString() << endl;
Complex cval = qscriptvalue_cast<Complex>(val);
cout << "qscriptvalue_cast : "<< cval << endl;
val = eng.evaluate("myObject.TestOutput(my_c)");
return 0;
}