Results 1 to 3 of 3

Thread: How to add class member function in QScriptEngine

  1. #1
    Join Date
    Aug 2012
    Location
    USSR
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to add class member function in QScriptEngine

    I need to add class member function in QScriptEngine.
    How to do this correctly?

    .h file
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4. private:
    5. QScriptEngine scriptEngine;
    6.  
    7. void setupFirst();
    8. public:
    9. QScriptValue appendDevice(QScriptContext *ctx, QScriptEngine *eng);
    10. };
    To copy to clipboard, switch view to plain text mode 


    .cpp file
    Qt Code:
    1. void MainWindow::setupFirst() {
    2.  
    3. this->scriptEngine.globalObject().setProperty("appendDevice",
    4. this->scriptEngine.newFunction(
    5. boost::bind(&MainWindow::appendDevice, this, _1, _2),
    6. 3)
    7. ); // How to do this correctly?
    8. }
    9.  
    10. QScriptValue MainWindow::appendDevice(QScriptContext *ctx, QScriptEngine *eng){
    11.  
    12. QString functionDescription = tr("Function format ") + "bool appendDevice(QString name, int address, const QString portName," +
    13. "const int rate = 115200," +
    14. "const QString config = \"\");";
    15. if (ctx->argumentCount() < 3){
    16. return ctx->throwError(QScriptContext::SyntaxError, tr("Unsufficient args count. ") + functionDescription);
    17. }
    18. QString name;
    19. int address;
    20. QString portName;
    21. if (ctx->argumentCount() >= 3){
    22. if (ctx->argument(0).isString()) {
    23. name = ctx->argument(0).toString();
    24. }
    25. else {
    26. return ctx->throwError(QScriptContext::TypeError, tr("Arg [0] (Device name) must be a string. ") + functionDescription);
    27. }
    28.  
    29. if (ctx->argument(1).isNumber()) {
    30. address = ctx->argument(1).toInt32();
    31. }
    32. else {
    33. throw ctx->throwError(QScriptContext::TypeError, tr("Arg [1] (Device address) must be a number. ") + functionDescription);
    34. }
    35.  
    36. if (ctx->argument(2).isString()) {
    37. portName = ctx->argument(2).toString();
    38. }
    39. else {
    40. throw ctx->throwError(QScriptContext::TypeError, tr("Arg [2] (Device port) must be a string. ") + functionDescription);
    41. }
    42. }
    43. int rate = QExtSerialPortAux::rateToInt(QExtSerialPortAux::defaultRate);
    44. QString config;
    45.  
    46. if (ctx->argumentCount() >= 4){
    47. if (ctx->argument(3).isNumber()) {
    48. rate = ctx->argument(3).toInt32();
    49. }
    50. else {
    51. throw ctx->throwError(QScriptContext::TypeError, tr("Arg [3] (Port baud rate) must be a number. ") + functionDescription);
    52. }
    53. }
    54. if (ctx->argumentCount() >= 5){
    55. if (ctx->argument(4).isString()) {
    56. config = ctx->argument(4).toString();
    57. }
    58. else {
    59. throw ctx->throwError(QScriptContext::TypeError, tr("Arg [4] (Device config) must be a string. ") + functionDescription);
    60. }
    61. }
    62.  
    63. return appendDeviceFromScript(name, address, portName, rate, config);
    64. }
    To copy to clipboard, switch view to plain text mode 

    Thank you for your replies.

  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: How to add class member function in QScriptEngine

    My suggestion is to create a QObject and QScriptable subclass that serves as your script API, put all function as public slots into it and then export that object to the script engine using newQObject().

    In your case, where you take a set of values from the script, I would even consider a small class/struct that can hold those values, make it known to the scriptengine using qScriptRegisterMetaType (see the example in its documentation) and then use this as the only parameter for the append function.

    The script would then look more like this
    Qt Code:
    1. var device = new Device();
    2. device.name = "device1"
    3. device.address = 1
    4.  
    5. // ..
    6.  
    7. program.appendDevice(device)
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Aug 2012
    Location
    USSR
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to add class member function in QScriptEngine

    Thank you.

Similar Threads

  1. Replies: 4
    Last Post: 1st February 2013, 11:16
  2. Replies: 2
    Last Post: 11th July 2012, 00:18
  3. Replies: 2
    Last Post: 28th March 2012, 21:47
  4. Replies: 2
    Last Post: 23rd February 2012, 12:23
  5. Replies: 3
    Last Post: 14th October 2008, 21:04

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.