QDeclarativeEngine Class

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

头: #include <QDeclarativeEngine>
Since: Qt 4.7
继承: QObject

公共类型

enum ObjectOwnership { CppOwnership, JavaScriptOwnership }

特性

公共函数

QDeclarativeEngine (QObject * parent = 0)
virtual ~QDeclarativeEngine ()
void addImageProvider (const QString & providerId , QDeclarativeImageProvider * provider )
void addImportPath (const QString & path )
void addPluginPath (const QString & path )
QUrl baseUrl () const
void clearComponentCache ()
QDeclarativeImageProvider * imageProvider (const QString & providerId ) const
QStringList importPathList () const
bool importPlugin (const QString & filePath , const QString & uri , QString * errorString )
QNetworkAccessManager * networkAccessManager () const
QDeclarativeNetworkAccessManagerFactory * networkAccessManagerFactory () const
QString offlineStoragePath () const
bool outputWarningsToStandardError () const
QStringList pluginPathList () const
void removeImageProvider (const QString & providerId )
QDeclarativeContext * rootContext () const
void setBaseUrl (const QUrl & url )
void setImportPathList (const QStringList & paths )
void setNetworkAccessManagerFactory (QDeclarativeNetworkAccessManagerFactory * factory )
void setOfflineStoragePath (const QString & dir )
void setOutputWarningsToStandardError (bool enabled )
void setPluginPathList (const QStringList & paths )

信号

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

静态公共成员

QDeclarativeContext * contextForObject (const QObject * object )
ObjectOwnership objectOwnership (QObject * object )
void setContextForObject (QObject * object , QDeclarativeContext * context )
void setObjectOwnership (QObject * object , ObjectOwnership ownership )
QDeclarativeInfo qmlInfo (const QObject * object )
int qmlRegisterInterface (const char * typeName )
int qmlRegisterType (const char * uri , int versionMajor , int versionMinor , const char * qmlName )
int qmlRegisterType ()
int qmlRegisterTypeNotAvailable (const char * uri , int versionMajor , int versionMinor , const char * qmlName , const QString & message )
int qmlRegisterUncreatableType (const char * uri , int versionMajor , int versionMinor , const char * qmlName , const QString & message )

QML_DECLARE_TYPE ()
QML_DECLARE_TYPEINFO ( Type , Flags )

额外继承成员

详细描述

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 根上下文 .

另请参阅 QDeclarativeComponent and QDeclarativeContext .

成员类型文档编制

enum 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 are:

常量 描述
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.

特性文档编制

offlineStoragePath : QString

This property holds the directory for storing offline user data.

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

QDeclarativeWebView 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 ().

访问函数:

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

成员函数文档编制

QDeclarativeEngine:: QDeclarativeEngine ( QObject * parent = 0)

创建新的 QDeclarativeEngine 采用给定 parent .

[虚拟] QDeclarativeEngine:: ~QDeclarativeEngine ()

销毁 QDeclarativeEngine .

任何 QDeclarativeContext 's created on this engine will be invalidated, but not destroyed (unless they are parented to the QDeclarativeEngine 对象)。

void QDeclarativeEngine:: addImageProvider (const QString & providerId , QDeclarativeImageProvider * provider )

设置 provider to use for images requested via the image : url scheme, with host providerId QDeclarativeEngine 拥有所有权对于 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 ().

void QDeclarativeEngine:: addImportPath (const QString & path )

添加 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 () 和 QML 模块 .

void QDeclarativeEngine:: addPluginPath (const QString & path )

添加 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 () 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 QDeclarativeComponent 构造函数。

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

另请参阅 setBaseUrl ().

void QDeclarativeEngine:: clearComponentCache ()

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.

[static] QDeclarativeContext * QDeclarativeEngine:: contextForObject (const QObject * object )

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

QDeclarativeEngine instantiates a QObject , the context is set automatically.

另请参阅 setContextForObject ().

QDeclarativeImageProvider * QDeclarativeEngine:: imageProvider (const QString & providerId ) const

返回 QDeclarativeImageProvider set for providerId .

QStringList QDeclarativeEngine:: importPathList () const

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/ for the 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 QDeclarativeEngine:: importPlugin (const QString & filePath , const QString & uri , QString * errorString )

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 () const

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 ; otherwise the returned QNetworkAccessManager will have no proxy or cache set.

另请参阅 setNetworkAccessManagerFactory ().

QDeclarativeNetworkAccessManagerFactory * QDeclarativeEngine:: networkAccessManagerFactory () const

返回当前 QDeclarativeNetworkAccessManagerFactory .

另请参阅 setNetworkAccessManagerFactory ().

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

Returns the ownership of object .

另请参阅 setObjectOwnership ().

bool QDeclarativeEngine:: outputWarningsToStandardError () const

Returns true if warning messages will be output to stderr in addition to being emitted by the warnings () 信号,否则 false。

默认值为 true。

另请参阅 setOutputWarningsToStandardError ().

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

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

[signal] void QDeclarativeEngine:: quit ()

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

void QDeclarativeEngine:: removeImageProvider (const QString & providerId )

移除 QDeclarativeImageProvider for providerId .

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

另请参阅 addImageProvider ().

QDeclarativeContext * QDeclarativeEngine:: rootContext () const

返回引擎的根上下文。

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.

void QDeclarativeEngine:: setBaseUrl (const QUrl & url )

Set the base URL for this engine to url .

另请参阅 baseUrl ().

[static] void QDeclarativeEngine:: setContextForObject ( QObject * object , QDeclarativeContext * context )

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

void QDeclarativeEngine:: setImportPathList (const QStringList & paths )

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 .

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

void QDeclarativeEngine:: setNetworkAccessManagerFactory ( QDeclarativeNetworkAccessManagerFactory * factory )

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

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

设置 ownership of object .

另请参阅 objectOwnership ().

void QDeclarativeEngine:: setOutputWarningsToStandardError ( bool enabled )

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

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

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

[signal] void QDeclarativeEngine:: warnings (const QList < QDeclarativeError > & warnings )

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

相关非成员

QDeclarativeInfo qmlInfo (const QObject * object )

Prints warning messages that include the file and line number for the specified QML object .

When QML types display warning messages, it improves traceability if they include the QML file and line number on which the particular instance was instantiated.

To include the file and line number, an object must be passed. If the file and line number is not available for that instance (either it was not instantiated by the QML engine or location information is disabled), "unknown location" will be used instead.

例如,

qmlInfo(object) << tr("component property is a write-once property");
					

prints

QML MyCustomType (unknown location): component property is a write-once property
					

int qmlRegisterInterface (const char * typeName )

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

#include < QtDeclarative > to use this function.

返回 QML 类型 ID。

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 .

返回 QML 类型 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 模块 called "com.mycompany.qmlcomponents":

#include <QtDeclarative>
...
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 template function registers the C++ type in the QML system. Instances of this type cannot be created from the QML system.

#include < QtDeclarative > to use this function.

返回 QML 类型 ID。

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

This function registers a type in the QML system with the name qmlName , in the library imported from uri having the version number composed from versionMajor and versionMinor , but any attempt to instantiate the type will produce the given error message .

Normally, the types exported by a module should be fixed. However, if a C++ type is not available, you should at least "reserve" the QML type name, and give the user of your module a meaningful error message.

返回 QML 类型 ID。

范例:

#ifdef NO_GAMES_ALLOWED
qmlRegisterTypeNotAvailable("MinehuntCore", 0, 1, "Game", "Get back to work, slacker!");
#else
qmlRegisterType<MinehuntGame>("MinehuntCore", 0, 1, "Game");
#endif
					

This will cause any QML which uses this module and attempts to use the type to produce an error message:

fun.qml: Get back to work, slacker!
   Game {
   ^
					

Without this, a generic "Game is not a type" message would be given.

#include < QtDeclarative > to use this function.

另请参阅 qmlRegisterUncreatableType ().

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.

返回 QML 类型 ID。

#include < QtDeclarative > to use this function.

另请参阅 qmlRegisterTypeNotAvailable ().

宏文档编制

QML_DECLARE_TYPE ()

相当于 Q_DECLARE_METATYPE(TYPE *) and Q_DECLARE_METATYPE(QDeclarativeListProperty<TYPE>)

#include < QtDeclarative > to use this macro.

QML_DECLARE_TYPEINFO ( Type , Flags )

声明额外特性为给定 Type 如描述通过指定 Flags .

Current the only supported type info is QML_HAS_ATTACHED_PROPERTIES which declares that the Type supports 附加特性 .

#include < QtDeclarative > to use this macro.