Results 1 to 10 of 10

Thread: How to interrupt QScriptValu::call()

  1. #1
    Join Date
    Jul 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Post How to interrupt QScriptValu::call()

    Hello,

    I have an application, in which a thread is executing a qscript function

    Qt Code:
    1. ExecuteThread::ExecuteThread(QString code, QObject *parent) :
    2. QThread(parent)
    3. {
    4. this->code = code;
    5. this->result = 0;
    6. ExecuteEngine = new QScriptEngine();
    7. }
    8.  
    9. void ExecuteThread::run(){
    10. ExecuteEngine = new QScriptEngine();
    11. QScriptValue fun = ExecuteEngine->evaluate("(" + code + ")");
    12. QScriptValue val = fun.call();
    13. result = val.toInteger();
    14. delete ExecuteEngine;
    15. }
    16.  
    17. int ExecuteThread::getResult(){
    18. return result;
    19. }
    20.  
    21. void ExecuteThread::stop(){
    22. ExecuteEngine->abortEvaluation(QScriptValue(-100));
    23. exit(0);
    24. }
    To copy to clipboard, switch view to plain text mode 

    Everything works fine, until there is an infinte loop in the QScript-function, then the thread stops at fun.call() and I can't delete the thread any more neither after calling stop() nor calling terminate(). Could you please tell me what I have to do in this case?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to interrupt QScriptValu::call()

    Search the forum, please. There has been a similar discussion few months ago.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to interrupt QScriptValu::call()

    Yes I know, I have seen it and it worked, but I wasn't able to get my return statement by the method they used in this thread: http://www.qtcentre.org/threads/5010...all()?p=225108

  4. #4
    Join Date
    Jul 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to interrupt QScriptValu::call()

    I meant this discussion of course: http://www.qtcentre.org/threads/4769...ptValue-call()

    Please help me

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to interrupt QScriptValu::call()

    If I knew another method, I would have suggested it in that thread. And I don't quite understand what you mean by that you couldn't get your return statement.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jul 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to interrupt QScriptValu::call()

    When I have for example the following qscript function:
    Qt Code:
    1. function(var xy){
    2. if(xy == 15){
    3. xy = xy*2;
    4. }
    5. var xx = xy*3;
    6. return xx;
    7. }
    To copy to clipboard, switch view to plain text mode 
    I can get the returned value by
    Qt Code:
    1. QScriptValue fun = ExecuteEngine->evaluate("(" + code + ")");
    2. QScriptValue val = fun.call();
    3. result = val.toInteger();
    To copy to clipboard, switch view to plain text mode 
    But how can I do it without using the call function and is that possible at all?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to interrupt QScriptValu::call()

    My suggestion from the other thread was not using call(). If you use evaluate(), it returns the result of the code it evaluates. Thus if you evaluate a function, it will return a result of that function.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jul 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to interrupt QScriptValu::call()

    Why won't it work then, if I use this code? result is 0 instead of 115

    Qt Code:
    1. ExecuteEngine = new QScriptEngine();
    2. QScriptContext *context = ExecuteEngine->pushContext();
    3. QScriptValue v = context->activationObject();
    4. v.setProperty("fun", "fun(){return 115;}");
    5. result = ExecuteEngine->evaluate("fun()").toInteger();
    6. ExecuteEngine->popContext();
    7. delete ExecuteEngine;
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to interrupt QScriptValu::call()

    This is what your code returns:

    "TypeError: Result of expression 'fun' [fun(){return 115;}] is not a function."
    so your function code is plain wrong

    Here is the proper code:

    Qt Code:
    1. #include <QtGui>
    2. #include <QtScript>
    3.  
    4. int main(int argc, char **argv) {
    5. QApplication app(argc, argv);
    6. QScriptEngine engine;
    7. QScriptContext *context = engine.pushContext();
    8. QScriptValue v = context->activationObject();
    9. v.setProperty("fun", engine.evaluate("(function(){return 115;})"));
    10. QScriptValue result = engine.evaluate("fun()");
    11. engine.popContext();
    12. qDebug() << result.toString();
    13. return 0;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jul 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to interrupt QScriptValu::call()

    Ah now I understand and it's working too.

    Thanks a lot

Similar Threads

  1. Serial data interrupt when minimizing a display window.
    By nagabathula in forum Qt Programming
    Replies: 8
    Last Post: 14th June 2011, 10:03
  2. Replies: 2
    Last Post: 7th June 2011, 09:24
  3. Sending OS I/O interrupt signals!
    By nofortee in forum Qt Programming
    Replies: 6
    Last Post: 13th July 2010, 16:42
  4. Interrupt/Exception caught while compiling Qt program
    By Qt Coder in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2010, 23:08
  5. Control-C to interrupt is not working
    By rburge in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2006, 22:18

Tags for this Thread

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.