Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-1742

Simplify registering self written QObjects

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Invalid
    • P3: Somewhat important
    • Some future release
    • 4.3.4
    • Qt Script
    • None

    Description

      It can be made easier to register a self written QObject type into a QScriptEngine. Below are some template functions to simplify this task. Eventually something similar can be integrated as macro or convenience function in further Qt versions.

      // template QObject factory callback function
      template<class T> QScriptValue scriptConstructorCallback(QScriptContext* context, QScriptEngine* engine)

      { return engine->newQObject(new T(), QScriptEngine::ScriptOwnership); }

      // register given template parameter as custom type in given script engine
      template<class T> void registerScriptType(QScriptEngine& engine, const QString* constructorName = 0)

      { QScriptValue constructor = engine.newFunction(scriptConstructorCallback<T>); QScriptValue value = engine.newQMetaObject(&T::staticMetaObject, constructor); engine.globalObject().setProperty((constructorName) ? *constructorName : value.toQMetaObject()->className(), value); }

      ...

      class MyType : public QObject

      { ... }

      ;

      // register custom type
      QScriptEngine engine;
      registerScriptType<MyType>(engine);

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            kenthans Kent Hansen (Inactive)
            sanonymous Nokia Qt Support (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes