Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Tous les espaces de nom  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

Creating QtScript Extensions

QtScript extensions can make additional functionality available to scripts evaluated by a QScriptEngine. Extensions are imported by calling the QScriptEngine::importExtension() function.

There are three ways to create an extension:

  • Subclass QScriptExtensionPlugin and implement the desired functionality.
  • Implement the functionality in a script file.
  • Use a hybrid approach, where part of the functionality is implemented in a QScriptExtensionPlugin, and part is implemented in a script file.

The (dot-qualified) extension name is used to determine the path (relative to the application's plugin path) where QScriptEngine will look for the script file that will initialize the extension; if a file called __init__.js (usually located in [application plugin path]/script/foo/) is found in the corresponding folder, its contents will be evaluated by the engine when the extension is imported. As an example, if the extension is called "foo.bar.baz", the engine will look for __init__.js in foo/bar/baz. Additionally, before importing "foo.bar.baz", the engine will ensure that the extensions "foo" and "foo.bar" are imported, locating and evaluating the corresponding __init__.js in the same manner (in folders foo and foo/bar, respectively).

The contents of __init__.js are evaluated in a new QScriptContext, as if it were the body of a function. The engine's Global Object acts as the this object. The following local variables are initially available to the script:

  • __extension__: The name of the extension (e.g. "foo.bar.baz").
  • __setupPackage__: A convenience function for setting up a "namespace" in the script environment. A typical application is to call __setupPackage__() with __extension__ as argument; e.g. __setupPackage__("foo.bar.baz") would ensure that the object chain represented by the expression foo.bar.baz exists in the script environment. (This function is semantically equivalent to QScriptExtensionPlugin::setupPackage().)
  • __postInit__: By default, this variable is undefined. If you assign a function to it, that function will be called after the C++ plugin's initialize() function has been called. You can use this to perform further initialization that depends on e.g. native functions that the C++ plugin registers.

An example of a simple __init__.js:

 print("importing " + __extension__);
 __setupPackage__("cool.stuff");

 cool.stuff.add = function(a, b) { return a + b; }
 cool.stuff.subtract = function(a, b) { return a - b; }

QScriptEngine will look for a QScriptExtensionPlugin that provides the relevant extension by querying each plugin for its keys() until a match is found. The plugin's initialize() function will be called after the relevant __init__.js (if any) has been evaluated.

Continuining with the example of our imaginary extension "foo.bar.baz", the following steps will be performed by QScriptEngine::importExtension():

  • If it exists, foo/__init__.js is evaluated.
  • If a plugin with "foo" in its list of keys is found, its initialize() function is called with "foo" as key.
  • If it exists, foo/bar/__init__.js is evaluated.
  • If a plugin with "foo.bar" in its list of keys is found, its initialize() function is called with "foo.bar" as key.
  • If it exists, foo/bar/baz/__init__.js is evaluated.
  • If a plugin with "foo.bar.baz" in its list of keys is found, its initialize() function is called with "foo.bar.baz" as key.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. Microsoft ouvre aux autres compilateurs C++ AMP, la spécification pour la conception d'applications parallèles C++ utilisant le GPU 22
  2. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  3. RIM : « 13 % des développeurs ont gagné plus de 100 000 $ sur l'AppWord », Qt et open-source au menu du BlackBerry DevCon Europe 0
  4. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 12
  5. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
Page suivante

Le Qt Developer Network au hasard

Logo

Compiler l'add-in Qt de Visual Studio

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

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.4
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 !
 
 
 
 
Partenaires

Hébergement Web