Results 1 to 6 of 6

Thread: How to use QScript?

  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to use QScript?

    Hi,

    I am looking at Qt's script example but got lost...

    I have several QVector<double> array in my C++ program with same length, such as

    QVector<double> aArray, bArray, cArray...

    I want users to be able to enter their own math expression, such as:

    result = aArray^2 + bArray / cArray.

    The operation is actually on each element. In C++, it would look like this:

    QVector<double> result( aArray.length() );
    for ( int idx = 0; idx < aArray.length(); idx++ ) {
    result [ idx ] = aArray[ idx ]^2 + bArray[ idx ] / cArray[ idx ].
    }

    Then the result should be returned to the C++ program.

    Can someone give me a light?

    Many thanks.

  2. #2
    Join Date
    Apr 2010
    Location
    Minsk, Republic of Belarus
    Posts
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use QScript?

    ECMA Script don't have operator overloading.
    First way, use QScript like Cpp:
    Qt Code:
    1. arrayA = [0.1, 0.4, 0.5]
    2. arrayB = [0.1, 0.1, 0.7]
    3. arrayC = [0.6, 0.23, 0.44]
    4. for ( var idx = 0; idx < aArray.length; ++idx) {
    5. result[idx] = aArray[idx]^2 + bArray[idx]/cArray[idx].
    6. }
    To copy to clipboard, switch view to plain text mode 
    Second way create functions for array prototype:
    Qt Code:
    1. Array.prototype.addition = function(other) {
    2. var result;
    3. for (var i; i < this.length; ++i) {
    4. result[i] = this[i] + other[i];
    5. }
    6. return result;
    7. }
    8. Array.prototype.division = function(other) {
    9. var result;
    10. for (var i; i < this.length; ++i) {
    11. result[i] = this[i]/other[i];
    12. }
    13. return result;
    14. }
    15. Array.prototype.power = function(number) {
    16. var result;
    17. for (var i; i < this.length; ++i) {
    18. result[i] = Math.pow(this[i], other[i]);
    19. }
    20. return result;
    21. }
    To copy to clipboard, switch view to plain text mode 
    Using
    Qt Code:
    1. arrayA = [0.1, 0.4, 0.5]
    2. arrayB = [0.1, 0.1, 0.7]
    3. arrayC = [0.6, 0.23, 0.44]
    4. result = (arrayA.power(2)).addition(bArray.division(cArray));
    5. //result[idx] = aArray[idx]^2 + bArray[idx]/cArray[idx]
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use QScript?

    And do not forget, that operator ^ is not a pow, but XOR

  4. #4
    Join Date
    Apr 2010
    Location
    Minsk, Republic of Belarus
    Posts
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use QScript?

    Yes, it's xor operator)

  5. #5
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to use QScript?

    But how to pass array from C++ to the script? The examples aArray, bArray, and cArray are created insider the script...

  6. #6
    Join Date
    Apr 2010
    Location
    Minsk, Republic of Belarus
    Posts
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use QScript?

    The simplest way:
    C++
    Qt Code:
    1. QScriptValue makeQSArray(QVector<double> array, QScriptEngine* engine)
    2. {
    3. QScriptValue result = engine->newArray(array.count());
    4. for (int i = 0; i < array.count(); ++i) {
    5. result.setProperty(i, array.at(i));
    6. }
    7. return result;
    8. }
    9.  
    10. ............................
    11. {
    12. QVector<double> arrayA;
    13. arrayA << 0.1 << 0.4;
    14. engine->globalObject()->setProperty(arrayA, makeQSArray(arrayA, engine));
    15. }
    16. .........................
    To copy to clipboard, switch view to plain text mode 
    Now, property 'arrayA' of globalObject is array.

  7. The following user says thank you to asvil for this useful post:

    lni (2nd August 2010)

Similar Threads

  1. qscript suspend function?
    By reneb in forum Qt Programming
    Replies: 3
    Last Post: 27th February 2010, 13:03
  2. QScript + SimpleClass not working
    By tdt in forum Qt Programming
    Replies: 0
    Last Post: 14th February 2010, 08:25
  3. QScript workbench in 4.3
    By jh in forum Qt Programming
    Replies: 3
    Last Post: 29th October 2009, 13:53
  4. QScript + get function call parameters
    By Fastman in forum Qt Programming
    Replies: 3
    Last Post: 1st August 2009, 15:30
  5. QVariantList from QScript?
    By musla in forum Qt Programming
    Replies: 0
    Last Post: 29th June 2009, 10:22

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.