QDeclarativeEngine Class Reference

[ QtDeclarative module]

The QDeclarativeEngine class provides an environment for instantiating QML components. 更多...

继承 QObject .

类型

方法

Static Methods

Qt Signals


详细描述

The QDeclarativeEngine class provides an environment for instantiating QML components.

每个 QML 组件被实例化在 QDeclarativeContext . QDeclarativeContext 's are essential for passing data to QML components. In QML, contexts are arranged hierarchically and this hierarchy is managed by the QDeclarativeEngine.

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

 QDeclarativeEngine engine;
 QDeclarativeComponent component(&engine);
 component.setData("import QtQuick 1.0\nText { text: \"Hello world!\" }", QUrl());
 QDeclarativeItem *item = qobject_cast<QDeclarativeItem *>(component.create());
 //add item to view, etc
 ...
			

In this case, the Text item will be created in the engine's 根上下文 .


类型文档编制

QDeclarativeEngine.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 是:

常量 描述
QDeclarativeEngine.CppOwnership 0 The 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 .
QDeclarativeEngine.JavaScriptOwnership 1 The 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 delete the object if there are no remaining JavaScript references to it and it has no QObject.parent (). 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 QDeclarativeCompnent.create() or QDeclarativeComponent.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.

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


方法文档编制

QDeclarativeEngine.__init__ ( self , QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Create a new QDeclarativeEngine with the given parent .

QDeclarativeEngine.addImageProvider ( self , QString  id , QDeclarativeImageProvider )

设置 provider to use for images requested via the image : url scheme, with host providerId QDeclarativeEngine takes ownership of provider .

Image providers enable support for pixmap and threaded image requests. See the QDeclarativeImageProvider 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.

另请参阅 removeImageProvider ().

QDeclarativeEngine.addImportPath ( self , QString  dir )

添加 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 ().

另请参阅 setImportPathList () and QML 模块 .

QDeclarativeEngine.addPluginPath ( self , QString  dir )

添加 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 ().

另请参阅 setPluginPathList ().

QUrl QDeclarativeEngine.baseUrl ( self )

Return the base URL for this engine. The base URL is only used to resolve components when a relative URL is passed to the QDeclarativeComponent 构造函数。

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

另请参阅 setBaseUrl ().

QDeclarativeEngine.clearComponentCache ( self )

Clears the engine's internal component cache.

Normally the QDeclarativeEngine caches components loaded from qml files. This method clears this cache and forces the component to be reloaded.

QDeclarativeContext QDeclarativeEngine.contextForObject ( QObject )

返回 QDeclarativeContext object , or 0 if no context has been set.

QDeclarativeEngine instantiates a QObject , the context is set automatically.

另请参阅 setContextForObject ().

QDeclarativeImageProvider QDeclarativeEngine.imageProvider ( self , QString  id )

返回 QDeclarativeImageProvider set for providerId .

QStringList QDeclarativeEngine.importPathList ( self )

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

例如,若 /opt/MyApp/lib/imports is in the path, then QML that imports com.mycompany.Feature will cause the QDeclarativeEngine to look in /opt/MyApp/lib/imports/com/mycompany/Feature/ 为 components provided by that module. A qmldir file is required for defining the type version mapping and possibly declarative 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 .

另请参阅 addImportPath () 和 setImportPathList ().

(bool, QString  errorString ) QDeclarativeEngine.importPlugin ( self , QString  filePath , QString  uri )

Imports the plugin named filePath 采用 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 QDeclarativeExtensionPlugin 接口。

QNetworkAccessManager QDeclarativeEngine.networkAccessManager ( self )

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

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

另请参阅 setNetworkAccessManagerFactory ().

QDeclarativeNetworkAccessManagerFactory QDeclarativeEngine.networkAccessManagerFactory ( self )

返回当前 QDeclarativeNetworkAccessManagerFactory .

另请参阅 setNetworkAccessManagerFactory ().

ObjectOwnership QDeclarativeEngine.objectOwnership ( QObject )

Returns the ownership of object .

另请参阅 setObjectOwnership ().

QString QDeclarativeEngine.offlineStoragePath ( self )

bool QDeclarativeEngine.outputWarningsToStandardError ( self )

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

默认值为 true。

另请参阅 setOutputWarningsToStandardError ().

QStringList QDeclarativeEngine.pluginPathList ( self )

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.

另请参阅 addPluginPath () 和 setPluginPathList ().

QDeclarativeEngine.removeImageProvider ( self , QString  id )

移除 QDeclarativeImageProvider for providerId .

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

另请参阅 addImageProvider ().

QDeclarativeContext QDeclarativeEngine.rootContext ( self )

返回引擎的根上下文。

The root context is automatically created by the QDeclarativeEngine . 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.

QDeclarativeEngine.setBaseUrl ( self , QUrl )

Set the base URL for this engine to url .

另请参阅 baseUrl ().

QDeclarativeEngine.setContextForObject ( QObject , QDeclarativeContext )

设置 QDeclarativeContext object to context 。若 object already has a context, a warning is output, but the context is not changed.

QDeclarativeEngine instantiates a QObject , the context is set automatically.

另请参阅 contextForObject ().

QDeclarativeEngine.setImportPathList ( self , QStringList  paths )

paths as the list of directories where the engine searches for installed modules in a URL-based directory 结构。

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 .

另请参阅 importPathList () 和 addImportPath ().

QDeclarativeEngine.setNetworkAccessManagerFactory ( self , QDeclarativeNetworkAccessManagerFactory )

设置 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.

另请参阅 networkAccessManagerFactory ().

QDeclarativeEngine.setObjectOwnership ( QObject , ObjectOwnership )

设置 ownership of object .

另请参阅 objectOwnership ().

QDeclarativeEngine.setOfflineStoragePath ( self , QString  dir )

QDeclarativeEngine.setOutputWarningsToStandardError ( self , bool)

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

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.

默认值为 true。

另请参阅 outputWarningsToStandardError ().

QDeclarativeEngine.setPluginPathList ( self , 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.

另请参阅 pluginPathList () 和 addPluginPath ().


Qt Signal Documentation

void quit ()

This is the default overload of this signal.

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

void warnings (const QList<QDeclarativeError>&)

This is the default overload of this signal.

此信号被发射当 warnings messages are generated by QML.