Results 1 to 6 of 6

Thread: Qt Script

  1. #1
    Join Date
    Jul 2016
    Posts
    3

    Default Qt Script

    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-sc...nce/#fun_cross
    this is my implementation,
    Qt Code:
    1. bool MathScript::cross(int a, int b)
    2. {
    3. bool ret = false;
    4.  
    5. if(a > b && aPrev < bPrev)
    6. ret = true;
    7.  
    8. if(a < b && aPrev > bPrev)
    9. ret = true;
    10.  
    11. return ret;
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. m_Engine = new QScriptEngine(this);
    2.  
    3. MathScript *scriptMath = new MathScript;
    4.  
    5. QScriptValue objectValueMath = m_Engine->newQObject(scriptMath);
    6. m_Engine->globalObject().setProperty("math", objectValueMath);
    7.  
    8. QScriptValue result = m_Engine->evaluate("math.cross(10, 10");
    9. qDebug() << "evaluate " << result.toBool();
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. context->callee().setProperty("Aprev", context->argument(0));
    To copy to clipboard, switch view to plain text mode 

    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);

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt Script

    I am not sure I understand the issue, why not save the values inside the Math object?

    Cheers,
    _

  3. #3
    Join Date
    Jul 2016
    Posts
    3

    Default Re: Qt Script

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. a = new Cross();
    8. b = new Cross();
    9.  
    10. a->CrossOver(10, 20);
    11. b->CrossOver(11, 22);
    12. }
    To copy to clipboard, switch view to plain text mode 

    This would be evaluation
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. a->CrossOver(30, 19);
    4. b->CrossOver(20, 22);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef CROSS_H
    2. #define CROSS_H
    3.  
    4. #include <QDebug>
    5. #include <QObject>
    6.  
    7. class Cross : public QObject
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. Cross();
    13. bool CrossOver(int a, int b);
    14.  
    15. signals:
    16.  
    17. public slots:
    18.  
    19. private:
    20. int prevA;
    21. int prevB;
    22. };
    23.  
    24. #endif // CROSS_H
    To copy to clipboard, switch view to plain text mode 

    // .cpp
    Qt Code:
    1. Cross::Cross()
    2. {
    3. prevA = 0;
    4. prevB = 0;
    5. }
    6.  
    7. bool Cross::CrossOver(int a, int b)
    8. {
    9. bool ret = false;
    10.  
    11. qDebug() << "a " << a;
    12. qDebug() << "b " << b;
    13. qDebug() << "prevA " << prevA;
    14. qDebug() << "prevB " << prevB;
    15.  
    16. if(a > b && prevA < prevB)
    17. ret = true;
    18.  
    19. prevA = a;
    20. prevB = b;
    21.  
    22. qDebug() << "Result is " << ret;
    23. qDebug() << "";
    24.  
    25. return ret;
    26. }
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt Script

    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,
    _

  5. #5
    Join Date
    Jul 2016
    Posts
    3

    Default Re: Qt Script

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt Script

    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,
    _

Similar Threads

  1. Qt script combined with java script
    By prachi kamble in forum Newbie
    Replies: 8
    Last Post: 16th April 2015, 13:30
  2. Qml non Gui script
    By codeman in forum Qt Quick
    Replies: 7
    Last Post: 21st July 2014, 11:32
  3. Using QT Script...
    By minhaz in forum Newbie
    Replies: 1
    Last Post: 18th August 2009, 22:29
  4. Can I include a script from script?
    By yycking in forum Qt Programming
    Replies: 1
    Last Post: 24th April 2009, 03:01
  5. Qt script
    By rajesh_clt3 in forum Qt Programming
    Replies: 2
    Last Post: 27th July 2007, 12:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.