如何使用 c++ QtScript 中的标准复数

发布于 2024-09-08 11:39:39 字数 386 浏览 2 评论 0原文

我尝试找出如何在 QtScript 中使用复数,以便可以从 QtScript 调用使用复数参数定义的插槽。用户还应该可以从脚本访问复数的基本代数(+、-、exp、...)。

只是为了说明,我想打电话的是:

#include<complex>
typedef complex<double> Complex;

class MyCppClass : public QObject
{
Q_OBJECT
public:
  ...
public slots:
void mySignal(Complex rCValue); !! <<== should be callable from QtScript
  ...
}

有什么想法吗?谢谢!

I try to find out how to use complex numbers in QtScripts such that slots defined with complex arguments can be called from a QtScript. Also basic algebra (+,-,exp, ... ) of complex-numbers should be accessible to the user from the script.

Just for illustration want I want to call is:

#include<complex>
typedef complex<double> Complex;

class MyCppClass : public QObject
{
Q_OBJECT
public:
  ...
public slots:
void mySignal(Complex rCValue); !! <<== should be callable from QtScript
  ...
}

Any ideas? Thx!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

带上头具痛哭 2024-09-15 11:39:39

我认为你必须在 QtScript 中实现复杂的代数(类似于 http://examples. oreilly.com/9781565923928/text/8-6.txt),然后修改 mySignal 以接受这样的对象。

I think you must implement complex algebra in QtScript (something like http://examples.oreilly.com/9781565923928/text/8-6.txt) and then modify mySignal to accept an object like this.

画骨成沙 2024-09-15 11:39:39

这不是最终的答案......因为如上所述,运算符 +、- 和 * 不能用于 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);
};

测试.cpp:

#include "test.h"
void TestClass::TestOutput(Complex rValue)
{
    cout << "received value "<< rValue << endl;
}

main.cpp:

#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;
}

It's not the final answer ... since as indicated above the operators +,- and * cannot be used for Complex quantities on the javascript side. But for those interested I'd like to share the following code pieces, which allow to trigger slots with complex arguments.

test.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);
};

test.cpp:

#include "test.h"
void TestClass::TestOutput(Complex rValue)
{
    cout << "received value "<< rValue << endl;
}

main.cpp:

#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文