Results 1 to 14 of 14

Thread: QtScript newFunction won't work

  1. #1
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default QtScript newFunction won't work

    I love the new QtScript module added to Qt 4.3, and I got the engine to do exactly what I wanted very quickly. But now I'm trying to add functions to the engine, specifically one that prints the arguments in a QTextEdit. I have looked at the documentation for QScriptEngine::newFunction, bit I just can't get it to work. Here is my code:
    Qt Code:
    1. QScriptValue printString(QScriptContext *context, QScriptEngine *engine){
    2. for(int i=0; i<context->argumentCount(); i++){
    3. //outputText->append(context->argument(i).toString());
    4. }
    5. return context->callee();
    6. }
    7. void MainWindow::setupScripts(){
    8. engine = new QScriptEngine();
    9. QScriptValue fun = engine->newFunction(printString);
    10. engine->globalObject().setProperty("print", fun, QScriptValue::ReadOnly | QScriptValue::Undeletable);
    11. }
    To copy to clipboard, switch view to plain text mode 
    Whenever the code is compiled, I get the following error:
    Qt Code:
    1. ...:error: no matching function for call to `QScriptEngine::newFunction(<unknow type>)`
    To copy to clipboard, switch view to plain text mode 

    Anyone know why this might be?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QtScript newFunction won't work

    Is printString() in the same .cpp file as MainWindow::setupScripts()?

  3. #3
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: QtScript newFunction won't work

    yup

    Wow, yup is too short for me to post just that

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QtScript newFunction won't work

    Quote Originally Posted by ihoss View Post
    yup
    Is that the first error you get? Do you have QT += script in your .pro file?

  5. #5
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: QtScript newFunction won't work

    It is the first error, and I do have QT += script in the pro file. The thing is that the code compiles if I remove the newFunction line, and the compiled program works like intended.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QtScript newFunction won't work

    Quote Originally Posted by ihoss View Post
    The thing is that the code compiles if I remove the newFunction line, and the compiled program works like intended.
    Is printString() defined before MainWindow::setupScripts()?

  7. #7
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: QtScript newFunction won't work

    I've uploaded the entire project so you can have a look at it. I have commented out the part that creates the new function. It's almost at the bottom of the cpp file.
    Attached Files Attached Files

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QtScript newFunction won't work

    The problem is that you have declared printString() method in MainWindow class and it takes precedence over printString() function. Just remove line #28 from mainwindow.h. Also you have to fix MainWindow::htmlize() -- it shouldn't return a reference.

  9. The following user says thank you to jacek for this useful post:

    ihoss (28th November 2007)

  10. #9
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: QtScript newFunction won't work

    Thanks a lot, I had no idea why it wouldn't work

  11. #10
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: QtScript newFunction won't work

    Hokay, so I got it to work, but not the way I intended. What I want is for the print function to print something to the QTextEdit called outputText. Since the printString function is called as a static function, it can't point to outputText. I have tried lots of ways to get it to work, but all of them seem to throw some errors. Any idea how I can fix it?

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QtScript newFunction won't work

    Quote Originally Posted by ihoss View Post
    What I want is for the print function to print something to the QTextEdit called outputText.
    You need a global variable or a singleton object to achieve this.

  13. #12
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: QtScript newFunction won't work

    Hmm, I tried that, but I got an error with the .o compiled object file. It kept saying that it couldn't find the object that I made static. I don't have the code here, but what I did was to make a static variable which pointed to the QTextEdit I wanted to change. Then I initialized it in the constructor. I think this might have caused the error, since it wasn't 100% certain that the variable was initialized before it was retrieved in the static function called by the QtScript function. I'll post the code as soon as I get home if you don't already know what I messed up

  14. #13
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: QtScript newFunction won't work

    Ok, so I worked around the problem by adding the instance of the current class, which only has one public slot, write, to the engine as an object called document. Then I added a script function called print that called document.write. It works This assumes that you only have one public slot. If you have lots of stuff that you want to hide, then create a new class which inherits from QObject and add that to the script engine. Here is the code:
    Qt Code:
    1. engine = new QScriptEngine();
    2. QScriptValue that = engine->newQObject(this, QScriptEngine::QtOwnership, QScriptEngine::ExcludeChildObjects
    3. | QScriptEngine::ExcludeSuperClassMethods | QScriptEngine::ExcludeSuperClassProperties);
    4. engine->globalObject().setProperty("document",that);
    5. engine->evaluate("function print(str){return document.write(str);};");
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 3rd December 2007 at 09:09. Reason: wrapped too long line

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QtScript newFunction won't work

    Quote Originally Posted by ihoss View Post
    Then I added a script function called print that called document.write. It works This assumes that you only have one public slot. If you have lots of stuff that you want to hide, then create a new class which inherits from QObject and add that to the script engine.
    This is a very good solution and quite similar to the singleton approach.

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
  •  
Qt is a trademark of The Qt Company.