Moving from QSA to Qt Script |
QSA Function | Notes about Equivalent Qt Script Functions |
---|---|
eval() | The eval function in QSA opened a new scope for code being executed in the eval function, so locally declared variables were not accessible outside. In Qt Script, the eval() function shares the current scope, making locally declared variables accessible outside the eval() call. |
debug() | This function is not available in Qt Script. Use print() instead. |
connect() | QSA had closures, meaning that a member function reference implicitly contained its this object. Qt Script does not support this. See the Qt Script documentation for details on using the connect function. |
String.arg() | This function is not available in Qt Script. Use replace() or concat() instead. |
String.argDec() | This function is not available in Qt Script. Use replace() or concat() instead. |
String.argInt() | This function is not available in Qt Script. Use replace() or concat() instead. |
String.argStr() | This function is not available in Qt Script. Use replace() or concat() instead. |
String.endsWith() | This function is not available in Qt Script. Use lastIndexOf() instead. |
String.find() | This function is not available in Qt Script. Use indexOf() instead. |
String.findRev() | This function is not available in Qt Script. Use lastIndexOf() and length instead. |
String.isEmpty() | This function is not available in Qt Script. Use length == 0 instead. |
String.left() | This function is not available in Qt Script. Use substring() instead. |
String.lower() | This function is not available in Qt Script. Use toLowerCase() instead. |
String.mid() | This function is not available in Qt Script. Use substring() instead. |
String.right() | This function is not available in Qt Script. Use substring() instead. |
String.searchRev() | This function is not available in Qt Script. Use search() / match() instead. |
String.startsWith() | This function is not available in Qt Script. Use indexOf() == 0 instead. |
String.upper() | This function is not available in Qt Script. Use toUpperCase() instead. |
RegExp.valid | This property is not available in Qt Script because it is not required; a SyntaxError exception is thrown for bad RegExp objects. |
RegExp.empty | This property is not available in Qt Script. Use toString().length == 0 instead. |
RegExp.matchedLength | This property is not available in Qt Script. RegExp.exec() returns an array whose size is the matched length. |
RegExp.capturedTexts | This property is not available in Qt Script. RegExp.exec() returns an array of captured texts. |
RegExp.search() | This function is not available in Qt Script. Use RegExp.exec() instead. |
RegExp.searchRev() | This function is not available in Qt Script. Use RegExp.exec() or String.search()/match() instead. |
RegExp.exactMatch() | This function is not available in Qt Script. Use RegExp.exec() instead. |
RegExp.pos() | This function is not available in Qt Script. Use String.match() instead. |
RegExp.cap() | This function is not available in Qt Script. RegExp.exec() returns an array of captured texts. |
QSA also defined some internal Qt API which is not present in Qt Script. The types provided by QSA which are not provided by Qt Script are:
QSA is more than just a scripting engine. It provides project management, an editor with completion and a minimalistic IDE to edit scriptable projects. Qt Script on the other hand is just a scripting engine. This means that equivalents to the classes QSEditor, QSScript, QSProject and QSWorkbench do not exist in Qt Script. QSA also provides some extension APIs through the QSUtilFactory and QSInputDialogFactory. There is also no equivalent to these classes in the Qt Script API.
There are two different ways of making QObjects accessible from scripts in QSA. The first method is via the QSInterpreter::addTransientObject() and QSProject::addObject() functions. In this case objects are added to the global namespace of the interpreter using their object names as the names of the variables.
QPushButton *button = new QPushButton(); button->setObjectName("button"); interpreter->addTransientObject(button);
The code above adds the button to the global namespace under the name "button". One obvious limitation here is that there is potential for either unnamed QObjects or objects whose names conflict. Qt Script provides a more flexible way of adding QObjects to the scripting environment.
QPushButton *button = new QPushButton(); QScriptValue scriptButton = engine.newQObject(button); engine.globalObject().setProperty("button", scriptButton);
In the code above we create a QPushButton and wrap it in a script value using the function, QScriptEngine::newQObject(). This gives us a script value that we put into the global object using the name "button". The concept of objects and properties discussed above is quite visible here in the public C++ API as well. We have no dependency on the object's name and we can also resolve name conflicts more gracefully. Here, we operate directly on QScriptValue objects. This is the actual object that is being passed around inside the script engine, so we actually have low-level access to the internal script data structures, far beyond that which is possible in QSA. Properties, signals and slots of the QObject are accessible to the scripter in Qt Script, just like in QSA.
The other way to expose QObjects in QSA was to create a QSObjectFactory that made it possible to instantiate QObjects from scripts.
Below is listed some code from the filter example in the QSA package.
ModuleFactory::ModuleFactory() { registerClass( "ImageSource", &ImgSource::staticMetaObject); ... } QObject *ModuleFactory::create( const QString &type, const QVariantList &, QObject * ) { if ( type == "ImageSource" ) return new ImgSource(); ... } ... interpreter.addObjectFactory(new ModuleFactory());
The equivalent in Qt Script is written in much the same way as constructors are written in scripts. We register a callback C++ function under the name "ImageSource" in the global namespace and return the QObject from this function:
QScriptValue construct_QPushButton(QScriptContext *, QScriptEngine *engine) { return engine->newQObject(new QPushButton()); } ... QScriptValue constructor = engine.newFunction(construct_QPushButton); QScriptValue value = engine.newQMetaObject(&QPushButton::staticMetaObject, constructor); engine.globalObject().setProperty("QPushButton", value);
In the Qt Script case we use the same approach that we use to expose a QObject, namely via QScriptEngine::newQObject(). This function also has the benefit that it is possible to specify if the QObject should expose properties and slots of its base class. It is also possible to specify custom ownership rules.
The reader might question why we don't add the constructor function directly into the namespace, but create a meta-object script value for it in addition. The plain function would certainly be good enough, but by creating a QMetaObject based constructor we get the enums on QPushButton for free in the QPushButton function object. Exposing enums in QSA is rather painful in comparison.
If we want to add more "static" data to the QPushButton type in Qt Script, we're free to add properties, similar to how we did for the script. It is also possible to add custom functions to a Qt Script QPushButton instance by setting more properties on it, such as making the setText() C++ function available. It is also possible to acheive this by installing a custom prototype, and be memory efficient, as discussed in the script example above.
In QSA, it was possible to expose non-QObjects to QSA by wrapping them in a QObject and using either QSWrapperFactory or QSObjectFactory to expose them. Deciding when to use each of these classes could be confusing, as one was used for script based construction and the other for wrapping function parameters and return values, but in essence they did exactly the same thing.
In Qt Script, providing access to QObjects and non-QObjects is done in the same way as shown above, by creating a constructor function, and by adding properties or a custom prototype to the constructed object.
QSA supported a hardcoded set of type mappings which covered most of the QVariant types, QObjects and primitives. For more complex type signatures, such as the template-based tool classes, it had rather limited support. Qt Script is significantly better at type mapping and will convert lists of template types into arrays of the appropriate types, given that all the types are declared to the meta-type system.
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 4.6 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com