PDA

View Full Version : Qt Script



upd
24th July 2016, 14:51
Hi, i have one problem i can't solve. I'm making options for user to write script in my app. I would like to implement function cross like the one on https://www.tradingview.com/study-script-reference/#fun_cross
this is my implementation,


bool MathScript::cross(int a, int b)
{
bool ret = false;

if(a > b && aPrev < bPrev)
ret = true;

if(a < b && aPrev > bPrev)
ret = true;

return ret;
}




m_Engine = new QScriptEngine(this);

MathScript *scriptMath = new MathScript;

QScriptValue objectValueMath = m_Engine->newQObject(scriptMath);
m_Engine->globalObject().setProperty("math", objectValueMath);

QScriptValue result = m_Engine->evaluate("math.cross(10, 10");
qDebug() << "evaluate " << result.toBool();


Now this, is working, but it is wrong. The problem is i need to store previous values, inside cross(), now sure how to do that. The code is evaluated, then new data comes in, so it has to store previous values.
I guess one solution would be to use


context->callee().setProperty("Aprev", context->argument(0));


but this would be working only for one math.cross();
but the user can enter multiple of them, example
var r1 = math.cross(1, 2);
var r2 = math.cross(3, 4);

anda_skoa
24th July 2016, 15:52
I am not sure I understand the issue, why not save the values inside the Math object?

Cheers,
_

upd
24th July 2016, 18:37
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

a = new Cross();
b = new Cross();

a->CrossOver(10, 20);
b->CrossOver(11, 22);
}


This would be evaluation


void MainWindow::on_pushButton_clicked()
{
a->CrossOver(30, 19);
b->CrossOver(20, 22);
}




#ifndef CROSS_H
#define CROSS_H

#include <QDebug>
#include <QObject>

class Cross : public QObject
{
Q_OBJECT

public:
Cross();
bool CrossOver(int a, int b);

signals:

public slots:

private:
int prevA;
int prevB;
};

#endif // CROSS_H


// .cpp


Cross::Cross()
{
prevA = 0;
prevB = 0;
}

bool Cross::CrossOver(int a, int b)
{
bool ret = false;

qDebug() << "a " << a;
qDebug() << "b " << b;
qDebug() << "prevA " << prevA;
qDebug() << "prevB " << prevB;

if(a > b && prevA < prevB)
ret = true;

prevA = a;
prevB = b;

qDebug() << "Result is " << ret;
qDebug() << "";

return ret;
}


I don't know how to make this for qscript.

anda_skoa
24th July 2016, 23:47
Ok, this makes this even more difficult to understand.

Now you have two objects of your Cross class?

And your script also needs two?

So that you get two pairs of prevA/prevB value?

Cheers,
_

upd
25th July 2016, 00:48
I don't know how to explain it better, the task is simple.
User write script i have to run it every few seconds.
He can write

bool res1 = cross(x, y);
bool res2 = cross(c, d);
etc..
as many times he want, and i need to do math behind it. Now each function of cross have to store previous values, as this is how cross is calculated. It's like this script will run in loop.
Now if user could write only one cross, i would simply store as static previous values inside cross function. But there can be multiple of them.

anda_skoa
25th July 2016, 09:32
From your original post and the comment #5 I would have guessed that the second call to cross() sees the values of the first call as prevA and prevB.
But then you posted comment #3 and make the two calls on separate objects.

So maybe instead of writing code that doesn't do what you need it to do, you could give an example with actual values for a, b, prevA and prevB?

Cheers,
_