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  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QQmlEngine Class

The QQmlEngine class provides an environment for instantiating QML components. More...

 #include <QQmlEngine>

Inherits: QJSEngine.

This class was introduced in Qt 5.0.

Public Types

enum ObjectOwnership { CppOwnership, JavaScriptOwnership }

Properties

  • 1 property inherited from QObject

Public Functions

QQmlEngine(QObject * parent = 0)
virtual ~QQmlEngine()
void addImageProvider(const QString & providerId, QQmlImageProviderBase * provider)
void addImportPath(const QString & path)
bool addNamedBundle(const QString & name, const QString & fileName)
void addPluginPath(const QString & path)
QUrl baseUrl() const
void clearComponentCache()
QQmlImageProviderBase * imageProvider(const QString & providerId) const
QStringList importPathList() const
bool importPlugin(const QString & filePath, const QString & uri, QString * errorString)
bool importPlugin(const QString & filePath, const QString & uri, QList<QQmlError> * errors)
QQmlIncubationController * incubationController() const
QNetworkAccessManager * networkAccessManager() const
QQmlNetworkAccessManagerFactory * networkAccessManagerFactory() const
QString offlineStoragePath() const
bool outputWarningsToStandardError() const
QStringList pluginPathList() const
void removeImageProvider(const QString & providerId)
QQmlContext * rootContext() const
void setBaseUrl(const QUrl & url)
void setImportPathList(const QStringList & paths)
void setIncubationController(QQmlIncubationController * controller)
void setNetworkAccessManagerFactory(QQmlNetworkAccessManagerFactory * factory)
void setOfflineStoragePath(const QString & dir)
void setOutputWarningsToStandardError(bool enabled)
void setPluginPathList(const QStringList & paths)
void trimComponentCache()
  • 8 public functions inherited from QJSEngine
  • 31 public functions inherited from QObject

Signals

void quit()
void warnings(const QList<QQmlError> & warnings)

Static Public Members

QQmlContext * contextForObject(const QObject * object)
ObjectOwnership objectOwnership(QObject * object)
void setContextForObject(QObject * object, QQmlContext * context)
void setObjectOwnership(QObject * object, ObjectOwnership ownership)
  • 11 static public members inherited from QObject

Reimplemented Protected Functions

virtual bool event(QEvent *)
  • 9 protected functions inherited from QObject

Related Non-Members

int qmlRegisterInterface(const char * typeName)
int qmlRegisterModuleApi(const char * uri, int versionMajor, int versionMinor, QJSValue(* ) ( QQmlEngine *, QJSEngine * ) callback)
int qmlRegisterRevision(const char * uri, int versionMajor, int versionMinor)
int qmlRegisterType(const char * uri, int versionMajor, int versionMinor, const char * qmlName)
int qmlRegisterType()
int qmlRegisterUncreatableType(const char * uri, int versionMajor, int versionMinor, const char * qmlName, const QString & message)

Macros

QML_DECLARE_TYPE()
QML_DECLARE_TYPEINFO( Type, Flags)

Additional Inherited Members

  • 1 public slot inherited from QObject
  • 9 protected functions inherited from QObject

Detailed Description

The QQmlEngine class provides an environment for instantiating QML components.

Each QML component is instantiated in a QQmlContext. QQmlContext's are essential for passing data to QML components. In QML, contexts are arranged hierarchically and this hierarchy is managed by the QQmlEngine.

Prior to creating any QML components, an application must have created a QQmlEngine to gain access to a QML context. The following example shows how to create a simple Text item.

 QQmlEngine engine;
 QQmlComponent component(&engine);
 component.setData("import QtQuick 2.0\nText { text: \"Hello world!\" }", QUrl());
 QQuickItem *item = qobject_cast<QQuickItem *>(component.create());

 //add item to view, etc
 ...

In this case, the Text item will be created in the engine's root context.

Note that the QtQuick 1 version is called QDeclarativeEngine.

See also QQmlComponent and QQmlContext.

Member Type Documentation

enum QQmlEngine::ObjectOwnership

Ownership controls whether or not QML automatically destroys the QObject when the object is garbage collected by the JavaScript engine. The two ownership options are:

ConstantValueDescription
QQmlEngine::CppOwnership0The object is owned by C++ code, and will never be deleted by QML. The JavaScript destroy() method cannot be used on objects with CppOwnership. This option is similar to QScriptEngine::QtOwnership.
QQmlEngine::JavaScriptOwnership1The object is owned by JavaScript. When the object is returned to QML as the return value of a method call or property access, QML will track it, and delete the object if there are no remaining JavaScript references to it and it has no QObject::parent(). An object tracked by one QQmlEngine will be deleted during that QQmlEngine's destructor, and thus JavaScript references between objects with JavaScriptOwnership from two different engines will not be valid after the deletion of one of those engines. This option is similar to QScriptEngine::ScriptOwnership.

Generally an application doesn't need to set an object's ownership explicitly. QML uses a heuristic to set the default object ownership. By default, an object that is created by QML has JavaScriptOwnership. The exception to this are the root objects created by calling QQmlComponent::create() or QQmlComponent::beginCreate() which have CppOwnership by default. The ownership of these root-level objects is considered to have been transferred to the C++ caller.

Objects not-created by QML have CppOwnership by default. The exception to this is objects returned from a C++ method call. The ownership of these objects is passed to JavaScript.

Calling setObjectOwnership() overrides the default ownership heuristic used by QML.

Property Documentation

offlineStoragePath : QString

This property holds the directory for storing offline user data.

Returns the directory where SQL and other offline storage is placed.

QQuickWebView and the SQL databases created with openDatabase() are stored here.

The default is QML/OfflineStorage in the platform-standard user application data directory.

Note that the path may not currently exist on the filesystem, so callers wanting to create new files at this location should create it first - see QDir::mkpath().

Access functions:

QString offlineStoragePath() const
void setOfflineStoragePath(const QString & dir)

Member Function Documentation

QQmlEngine::QQmlEngine(QObject * parent = 0)

Create a new QQmlEngine with the given parent.

QQmlEngine::~QQmlEngine() [virtual]

Destroys the QQmlEngine.

Any QQmlContext's created on this engine will be invalidated, but not destroyed (unless they are parented to the QQmlEngine object).

void QQmlEngine::addImageProvider(const QString & providerId, QQmlImageProviderBase * provider)

Sets the provider to use for images requested via the image: url scheme, with host providerId. The QQmlEngine takes ownership of provider.

Image providers enable support for pixmap and threaded image requests. See the QQuickImageProvider documentation for details on implementing and using image providers.

All required image providers should be added to the engine before any QML sources files are loaded.

See also removeImageProvider(), QQuickImageProvider, and QQmlImageProviderBase.

void QQmlEngine::addImportPath(const QString & path)

Adds path as a directory where the engine searches for installed modules in a URL-based directory structure. The path may be a local filesystem directory or a URL.

The newly added path will be first in the importPathList().

See also setImportPathList() and QML Modules.

bool QQmlEngine::addNamedBundle(const QString & name, const QString & fileName)

void QQmlEngine::addPluginPath(const QString & path)

Adds path as a directory where the engine searches for native plugins for imported modules (referenced in the qmldir file).

By default, the list contains only ., i.e. the engine searches in the directory of the qmldir file itself.

The newly added path will be first in the pluginPathList().

See also setPluginPathList().

QUrl QQmlEngine::baseUrl() const

Return the base URL for this engine. The base URL is only used to resolve components when a relative URL is passed to the QQmlComponent constructor.

If a base URL has not been explicitly set, this method returns the application's current working directory.

See also setBaseUrl().

void QQmlEngine::clearComponentCache()

Clears the engine's internal component cache.

This function causes the property metadata of all components previously loaded by the engine to be destroyed. All previously loaded components and the property bindings for all extant objects created from those components will cease to function.

This function returns the engine to a state where it does not contain any loaded component data. This may be useful in order to reload a smaller subset of the previous component set, or to load a new version of a previously loaded component.

Once the component cache has been cleared, components must be loaded before any new objects can be created.

See also trimComponentCache().

QQmlContext * QQmlEngine::contextForObject(const QObject * object) [static]

Returns the QQmlContext for the object, or 0 if no context has been set.

When the QQmlEngine instantiates a QObject, the context is set automatically.

See also setContextForObject().

bool QQmlEngine::event(QEvent *) [virtual protected]

QQmlImageProviderBase * QQmlEngine::imageProvider(const QString & providerId) const

Returns the image provider set for providerId.

Returns the provider if it was found; otherwise returns 0.

See also QQuickImageProvider.

QStringList QQmlEngine::importPathList() const

Returns the list of directories where the engine searches for installed modules in a URL-based directory structure.

For example, if /opt/MyApp/lib/imports is in the path, then QML that imports com.mycompany.Feature will cause the QQmlEngine to look in /opt/MyApp/lib/imports/com/mycompany/Feature/ for the components provided by that module. A qmldir file is required for defining the type version mapping and possibly QML extensions plugins.

By default, the list contains the directory of the application executable, paths specified in the QML_IMPORT_PATH environment variable, and the builtin ImportsPath from QLibraryInfo.

See also addImportPath() and setImportPathList().

bool QQmlEngine::importPlugin(const QString & filePath, const QString & uri, QString * errorString)

Imports the plugin named filePath with the uri provided. Returns true if the plugin was successfully imported; otherwise returns false.

On failure and if non-null, *errorString will be set to a message describing the failure.

The plugin has to be a Qt plugin which implements the QQmlExtensionPlugin interface.

bool QQmlEngine::importPlugin(const QString & filePath, const QString & uri, QList<QQmlError> * errors)

Imports the plugin named filePath with the uri provided. Returns true if the plugin was successfully imported; otherwise returns false.

On failure and if non-null, the errors list will have any errors which occurred prepended to it.

The plugin has to be a Qt plugin which implements the QQmlExtensionPlugin interface.

QQmlIncubationController * QQmlEngine::incubationController() const

Returns the currently set incubation controller, or 0 if no controller has been set.

See also setIncubationController().

QNetworkAccessManager * QQmlEngine::networkAccessManager() const

Returns a common QNetworkAccessManager which can be used by any QML element instantiated by this engine.

If a QQmlNetworkAccessManagerFactory has been set and a QNetworkAccessManager has not yet been created, the QQmlNetworkAccessManagerFactory will be used to create the QNetworkAccessManager; otherwise the returned QNetworkAccessManager will have no proxy or cache set.

See also setNetworkAccessManagerFactory().

QQmlNetworkAccessManagerFactory * QQmlEngine::networkAccessManagerFactory() const

Returns the current QQmlNetworkAccessManagerFactory.

See also setNetworkAccessManagerFactory().

ObjectOwnership QQmlEngine::objectOwnership(QObject * object) [static]

Returns the ownership of object.

See also setObjectOwnership().

bool QQmlEngine::outputWarningsToStandardError() const

Returns true if warning messages will be output to stderr in addition to being emitted by the warnings() signal, otherwise false.

The default value is true.

See also setOutputWarningsToStandardError().

QStringList QQmlEngine::pluginPathList() const

Returns the list of directories where the engine searches for native plugins for imported modules (referenced in the qmldir file).

By default, the list contains only ., i.e. the engine searches in the directory of the qmldir file itself.

See also addPluginPath() and setPluginPathList().

void QQmlEngine::quit() [signal]

This signal is emitted when the QML loaded by the engine would like to quit.

void QQmlEngine::removeImageProvider(const QString & providerId)

Removes the image provider for providerId.

See also addImageProvider() and QQuickImageProvider.

QQmlContext * QQmlEngine::rootContext() const

Returns the engine's root context.

The root context is automatically created by the QQmlEngine. Data that should be available to all QML component instances instantiated by the engine should be put in the root context.

Additional data that should only be available to a subset of component instances should be added to sub-contexts parented to the root context.

void QQmlEngine::setBaseUrl(const QUrl & url)

Set the base URL for this engine to url.

See also baseUrl().

void QQmlEngine::setContextForObject(QObject * object, QQmlContext * context) [static]

Sets the QQmlContext for the object to context. If the object already has a context, a warning is output, but the context is not changed.

When the QQmlEngine instantiates a QObject, the context is set automatically.

See also contextForObject().

void QQmlEngine::setImportPathList(const QStringList & paths)

Sets paths as the list of directories where the engine searches for installed modules in a URL-based directory structure.

By default, the list contains the directory of the application executable, paths specified in the QML_IMPORT_PATH environment variable, and the builtin ImportsPath from QLibraryInfo.

See also importPathList() and addImportPath().

void QQmlEngine::setIncubationController(QQmlIncubationController * controller)

Sets the engine's incubation controller. The engine can only have one active controller and it does not take ownership of it.

See also incubationController().

void QQmlEngine::setNetworkAccessManagerFactory(QQmlNetworkAccessManagerFactory * factory)

Sets the factory to use for creating QNetworkAccessManager(s).

QNetworkAccessManager is used for all network access by QML. By implementing a factory it is possible to create custom QNetworkAccessManager with specialized caching, proxy and cookie support.

The factory must be set before executing the engine.

See also networkAccessManagerFactory().

void QQmlEngine::setObjectOwnership(QObject * object, ObjectOwnership ownership) [static]

Sets the ownership of object.

See also objectOwnership().

void QQmlEngine::setOutputWarningsToStandardError(bool enabled)

Set whether warning messages will be output to stderr to enabled.

If enabled is true, any warning messages generated by QML will be output to stderr and emitted by the warnings() signal. If enabled is false, on the warnings() signal will be emitted. This allows applications to handle warning output themselves.

The default value is true.

See also outputWarningsToStandardError().

void QQmlEngine::setPluginPathList(const QStringList & paths)

Sets the list of directories where the engine searches for native plugins for imported modules (referenced in the qmldir file) to paths.

By default, the list contains only ., i.e. the engine searches in the directory of the qmldir file itself.

See also pluginPathList() and addPluginPath().

void QQmlEngine::trimComponentCache()

Trims the engine's internal component cache.

This function causes the property metadata of any loaded components which are not currently in use to be destroyed.

A component is considered to be in use if there are any extant instances of the component itself, any instances of other components that use the component, or any objects instantiated by any of those components.

See also clearComponentCache().

void QQmlEngine::warnings(const QList<QQmlError> & warnings) [signal]

This signal is emitted when warnings messages are generated by QML.

Related Non-Members

int qmlRegisterInterface(const char * typeName)

This template function registers the C++ type in the QML system under the name typeName.

#include <QtQml> to use this function.

Returns the QML type id.

int qmlRegisterModuleApi(const char * uri, int versionMajor, int versionMinor, QJSValue(* ) ( QQmlEngine *, QJSEngine * ) callback)

This function may be used to register a module API provider callback in a particular uri with a version specified in versionMajor and versionMinor.

Installing a module API into a uri allows developers to provide arbitrary functionality (methods and properties) in a namespace that doesn't necessarily contain elements.

A module API may be either a QObject or a QJSValue. Only one module API provider may be registered into any given namespace (combination of uri, versionMajor and versionMinor). This function should be used to register a module API provider function which returns a QJSValue as a module API.

NOTE: QJSValue module API properties will not trigger binding re-evaluation if changed.

Usage:

 // first, define the module API provider function (callback).
 static QJSValue *example_qjsvalue_module_api_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
 {
     Q_UNUSED(engine)

     static int seedValue = 5;
     QJSValue example = scriptEngine->newObject();
     example.setProperty("someProperty", seedValue++);
     return example;
 }

 // second, register the module API provider with QML by calling this function in an initialization function.
 ...
 qmlRegisterModuleApi("Qt.example.qjsvalueApi", 1, 0, example_qjsvalue_module_api_provider);
 ...

In order to use the registered module API in QML, you must import the module API.

 import QtQuick 2.0
 import Qt.example.qjsvalueApi 1.0 as ExampleApi
 Item {
     id: root
     property int someValue: ExampleApi.someProperty
 }

int qmlRegisterRevision(const char * uri, int versionMajor, int versionMinor)

This template function registers the specified revision of a C++ type in the QML system with the library imported from uri having the version number composed from versionMajor and versionMinor.

Returns the QML type id.

 template<typename T, int metaObjectRevision>
 int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor);

This function is typically used to register the revision of a base class to use for the specified module version (see QML Type Versioning).

int qmlRegisterType(const char * uri, int versionMajor, int versionMinor, const char * qmlName)

This template function registers the C++ type in the QML system with the name qmlName, in the library imported from uri having the version number composed from versionMajor and versionMinor.

Returns the QML type id.

There are two forms of this template function:

 template<typename T>
 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName);

 template<typename T, int metaObjectRevision>
 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName);

The former is the standard form which registers the type T as a new type. The latter allows a particular revision of a class to be registered in a specified version (see QML Type Versioning).

For example, this registers a C++ class MySliderItem as a QML type named Slider for version 1.0 of a module called "com.mycompany.qmlcomponents":

 #include <QtQml>

 ...

 qmlRegisterType<MySliderItem>("com.mycompany.qmlcomponents", 1, 0, "Slider");

Once this is registered, the type can be used in QML by importing the specified module name and version number:

 import com.mycompany.qmlcomponents 1.0

 Slider {
     // ...
 }

Note that it's perfectly reasonable for a library to register types to older versions than the actual version of the library. Indeed, it is normal for the new library to allow QML written to previous versions to continue to work, even if more advanced versions of some of its types are available.

int qmlRegisterType()

This is an overloaded function.

This template function registers the C++ type in the QML system. Instances of this type cannot be created from the QML system.

#include <QtQml> to use this function.

Returns the QML type id.

int qmlRegisterUncreatableType(const char * uri, int versionMajor, int versionMinor, const char * qmlName, const QString & message)

This template function registers the C++ type in the QML system with the name qmlName, in the library imported from uri having the version number composed from versionMajor and versionMinor.

While the type has a name and a type, it cannot be created, and the given error message will result if creation is attempted.

This is useful where the type is only intended for providing attached properties or enum values.

Returns the QML type id.

#include <QtQml> to use this function.

See also qmlRegisterTypeNotAvailable().

Macro Documentation

QML_DECLARE_TYPE()

Equivalent to Q_DECLARE_METATYPE(TYPE *) and Q_DECLARE_METATYPE(QQmlListProperty<TYPE>)

#include <QtQml> to use this macro.

QML_DECLARE_TYPEINFO( Type, Flags)

Declares additional properties of the given Type as described by the specified Flags.

Current the only supported type info is QML_HAS_ATTACHED_PROPERTIES which declares that the Type supports Attached Properties.

#include <QtQml> to use this macro.

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 5.0-snapshot
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