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 use enums declared in QObject in QScript JavaScript?
Forum Updated to NodeBB v4.3 + New Features

How to use enums declared in QObject in QScript JavaScript?

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 720 Views 1 Watching
  • 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.
  • K Offline
    K Offline
    kuschky
    wrote on last edited by
    #1

    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
    {
      
    }
    

    }
    @

    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