How to use enums declared in QObject in QScript JavaScript?



  • I have written an application which can use a QOject based class declared and instanced on the C++ side of an application which is using the QScriptEngine. I'm using Qt 5.3.2 on a linux x86 machine.

    Everything is working well except that the enums which are included in my classes are not evaluated on in the QScripts. If somebody can explain me what I'm doing wrong I would be very glad.

    Here is how my C++ code looks

    @
    Class Task : public QObject
    {
    Q_OBJECT
    Q_ENUMS(TaskStatus)

    public:
    explicit Task(QObject *parent = 0);

    enum TaskStatus  {
        TASK_STATUS_OK   = 0,
        TASK_STATUS_FAIL = 1
    };
    

    signals:

    public slots:

    private:

    };

    Q_DECLARE_METATYPE(Task::TaskStatus)
    @

    Constructor for meta object

    @
    QScriptValue TaskObjectConstructor(QScriptContext *context, QScriptEngine *engine)
    {
    QObject *parent = context->argument(0).toQObject();
    QObject *object = new Task(parent);
    return engine->newQObject(object, QScriptEngine::ScriptOwnership);
    }
    @

    here I make the Objects visible to the QScriptEngine

    @
    QScriptValue ctor = this->newFunction(TaskObjectConstructor);
    QScriptValue metaObject = this->newQMetaObject(&QObject::staticMetaObject, ctor);
    this->globalObject().setProperty("Task", metaObject);
    @

    and here is the QScript part which is evaluated by the QScriptEngine

    @
    {
    var te = new Task() // works

    var v1 = Task.TASK_STATUS_OK;  // does not throw an error but v1 is still "undefined". If I change TASK_STATUS_OK to TASK_STATUS_ABC it complains about unknown variable
    
    if (v1 === Task.TASK_STATUS_OK)  // does not throw an error but comparsion is against v1 is which is undefined so it fails by missing evaluation
    {
      
    }
    

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.