Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add class member function in QScriptEngine
QtWS25 Last Chance

How to add class member function in QScriptEngine

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sacha_D
    wrote on last edited by
    #1

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

    .h file
    @class MainWindow : public QMainWindow
    {
    Q_OBJECT
    private:
    QScriptEngine scriptEngine;

      void setupFirst();
    

    public:
    QScriptValue appendDevice(QScriptContext *ctx, QScriptEngine *eng);
    };@

    .cpp file
    @void MainWindow::setupFirst() {

    this->scriptEngine.globalObject().setProperty("appendDevice",
    this->scriptEngine.newFunction(
    boost::bind(&MainWindow::appendDevice, this, _1, _2),
    3)
    ); // How to do this correctly?
    }

    QScriptValue MainWindow::appendDevice(QScriptContext *ctx, QScriptEngine *eng){

    QString functionDescription = tr("Function format ") + "bool appendDevice(QString name, int address, const QString portName," +
            "const int rate = 115200," +
            "const QString config = \"\");";
    if (ctx->argumentCount() < 3){
        return ctx->throwError(QScriptContext::SyntaxError, tr("Unsufficient args count. ") + functionDescription);
    }
    QString name;
    int address;
    QString portName;
    if (ctx->argumentCount() >= 3){
        if (ctx->argument(0).isString()) {
            name = ctx->argument(0).toString();
        }
        else {
            return ctx->throwError(QScriptContext::TypeError, tr("Arg [0] (Device name) must be a string. ") + functionDescription);
        }
    
        if (ctx->argument(1).isNumber()) {
            address = ctx->argument(1).toInt32();
        }
        else {
            throw ctx->throwError(QScriptContext::TypeError, tr("Arg [1] (Device address) must be a number. ") + functionDescription);
        }
    
        if (ctx->argument(2).isString()) {
            portName = ctx->argument(2).toString();
        }
        else {
            throw ctx->throwError(QScriptContext::TypeError, tr("Arg [2] (Device port) must be a string. ") + functionDescription);
        }
    }
    int rate  = QExtSerialPortAux::rateToInt(QExtSerialPortAux::defaultRate);
    QString config;
    
    if (ctx->argumentCount() >= 4){
        if (ctx->argument(3).isNumber()) {
            rate = ctx->argument(3).toInt32();
        }
        else {
            throw ctx->throwError(QScriptContext::TypeError, tr("Arg [3] (Port baud rate) must be a number. ") + functionDescription);
        }
    }
    if (ctx->argumentCount() >= 5){
        if (ctx->argument(4).isString()) {
            config = ctx->argument(4).toString();
        }
        else {
            throw ctx->throwError(QScriptContext::TypeError, tr("Arg [4] (Device config) must be a string. ") + functionDescription);
        }
    }
    
    return appendDeviceFromScript(name, address, portName, rate, config);
    

    }
    @

    Thank you for your replies.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AcerExtensa
      wrote on last edited by
      #2

      MainWindow::appendDevice should be static function, then you can do what.

      God is Real unless explicitly declared as Integer.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved