Results 1 to 4 of 4

Thread: QJSEngine call method with variable arguments number

  1. #1
    Join Date
    Mar 2015
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QJSEngine call method with variable arguments number

    Hi,

    I use QJSEngine and I want to add to the engine a method with variable arguments number.

    I have added methods of C++ class "CustomScripts" with this code and it works fine, I can call myFunction.
    Qt Code:
    1. CustomScripts * scriptObject = new CustomScripts;
    2. QJSValue scriptTest = myQJSEngine->newQObject(scriptObject);
    3. QJSValue aFunction = scriptTest.property("myFunction");
    4. myQJSEngine->globalObject().setProperty("myFunction", aFunction);
    To copy to clipboard, switch view to plain text mode 
    But exposed methods take only 1 or 2 arguments.

    I try to use QVariantList to pass variable argument number but it only work for javascript array.
    How can I do that ?

  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: QJSEngine call method with variable arguments number

    Is the method written in C++ or JavaScript? How are you trying to call it?
    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
    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: QJSEngine call method with variable arguments number

    In QtScript you could do that through QScriptContext.

    QJSEngine doesn't any of the integration API (yet), so you probably have to work around these limitations.
    Maybe create a function in JavaScript that converts alls its arguments into an array and then calls the C++ function.

    Cheers,
    _

  4. The following user says thank you to anda_skoa for this useful post:

    GuillaumeG (2nd April 2015)

  5. #4
    Join Date
    Mar 2015
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QJSEngine call method with variable arguments number

    Thanks, your right.

    If I use a javascript method to wrap the C++ callback, it works.
    Here my code, with "printCallback" a C++ method exposed in javascript.
    Qt Code:
    1. QJSValue print = fEngine->evaluate("function() { printCallback(Array.prototype.slice.apply(arguments));}");
    2. fEngine->globalObject().setProperty("print", print);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 3
    Last Post: 13th March 2015, 15:09
  2. Replies: 13
    Last Post: 12th November 2013, 20:35
  3. slot with variable arguments
    By kung-foo in forum Qt Programming
    Replies: 1
    Last Post: 25th November 2009, 19:24
  4. qmake - how to extract number from variable
    By Vanir in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2009, 18:12
  5. Replies: 1
    Last Post: 6th April 2006, 12:24

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.