The QDeclarativeComponent class encapsulates a QML component definition. 更多...
| 头: | #include <QDeclarativeComponent> |
| Since: | Qt 4.7 |
| 实例化: | Component |
| 继承: | QObject |
| enum | Status { Null, Ready, Loading, Error } |
| QDeclarativeComponent (QDeclarativeEngine * engine , QObject * parent = 0) | |
| QDeclarativeComponent (QDeclarativeEngine * engine , const QString & fileName , QObject * parent = 0) | |
| QDeclarativeComponent (QDeclarativeEngine * engine , const QUrl & url , QObject * parent = 0) | |
| virtual | ~QDeclarativeComponent () |
| virtual QObject * | beginCreate (QDeclarativeContext * context ) |
| virtual void | completeCreate () |
| virtual QObject * | create (QDeclarativeContext * context = 0) |
| QDeclarativeContext * | creationContext () const |
| QList<QDeclarativeError> | errors () const |
| bool | isError () const |
| bool | isLoading () const |
| bool | isNull () const |
| bool | isReady () const |
| void | loadUrl (const QUrl & url ) |
| qreal | progress () const |
| void | setData (const QByteArray & data , const QUrl & url ) |
| 状态 | status () const |
| QUrl | url () const |
| void | progressChanged (qreal progress ) |
| void | statusChanged (QDeclarativeComponent::Status status ) |
The QDeclarativeComponent class encapsulates a QML component definition.
Components are reusable, encapsulated QML elements with well-defined interfaces. They are often defined in Component Files .
A
QDeclarativeComponent
instance can be created from a QML file. For example, if there is a
main.qml
文件像这样:
import QtQuick 1.0 Item { width: 200 height: 200 }
The following code loads this QML file as a component, creates an instance of this component using create (), and then queries the Item 's width 值:
QDeclarativeEngine *engine = new QDeclarativeEngine; QDeclarativeComponent component(engine, QUrl::fromLocalFile("main.qml")); QObject *myObject = component.create(); QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(myObject); int width = item->width(); // width = 200
If the URL passed to QDeclarativeComponent is a network resource, or if the QML document references a network resource, the QDeclarativeComponent has to fetch the network data before it is able to create objects. In this case, the QDeclarativeComponent will have a Loading status . An application will have to wait until the component is Ready before calling QDeclarativeComponent::create ().
The following example shows how to load a QML file from a network resource. After creating the
QDeclarativeComponent
, it tests whether the component is loading. If it is, it connects to the
QDeclarativeComponent::statusChanged
() signal and otherwise calls the
continueLoading()
method directly. Note that
QDeclarativeComponent::isLoading
() may be false for a network component if the component has been cached and is ready immediately.
MyApplication::MyApplication() { // ... component = new QDeclarativeComponent(engine, QUrl("http://www.example.com/main.qml")); if (component->isLoading()) QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), this, SLOT(continueLoading())); else continueLoading(); } void MyApplication::continueLoading() { if (component->isError()) { qWarning() << component->errors(); } else { QObject *myObject = component->create(); } }
另请参阅 在 C++ 应用程序中使用 QML 绑定 and 集成 QML 代码与现有 Qt UI 代码 .
Specifies the loading status of the QDeclarativeComponent .
| 常量 | 值 | 描述 |
|---|---|---|
QDeclarativeComponent::Null
|
0
|
This QDeclarativeComponent has no data. Call loadUrl () 或 setData () to add QML content. |
QDeclarativeComponent::Ready
|
1
|
This QDeclarativeComponent is ready and create () may be called. |
QDeclarativeComponent::Loading
|
2
|
This QDeclarativeComponent is loading network data. |
QDeclarativeComponent::Error
|
3
|
An error has occurred. Call errors () to retrieve a list of { QDeclarativeError }{errors}. |
The progress of loading the component, from 0.0 (nothing loaded) to 1.0 (finished).
访问函数:
| qreal | progress () const |
通知程序信号:
| void | progressChanged (qreal progress ) |
The component's current status .
访问函数:
| 状态 | status () const |
通知程序信号:
| void | statusChanged (QDeclarativeComponent::Status status ) |
The component URL. This is the URL passed to either the constructor, or the loadUrl () 或 setData () 方法。
访问函数:
| QUrl | url () const |
创建 QDeclarativeComponent with no data and give it the specified engine and parent . Set the data with setData ().
创建 QDeclarativeComponent 从给定 fileName and give it the specified parent and engine .
另请参阅 loadUrl ().
创建 QDeclarativeComponent 从给定 url and give it the specified parent and engine .
Ensure that the URL provided is full and correct, in particular, use QUrl::fromLocalFile () when loading a file from the local filesystem.
另请参阅 loadUrl ().
[虚拟]
QDeclarativeComponent::
~QDeclarativeComponent
()
Destruct the QDeclarativeComponent .
[虚拟]
QObject
* QDeclarativeComponent::
beginCreate
(
QDeclarativeContext
*
context
)
This method provides more advanced control over component instance creation. In general, programmers should use QDeclarativeComponent::create () to create a component.
Create an object instance from this component. Returns 0 if creation failed. context specifies the context within which to create the object instance.
当 QDeclarativeComponent constructs an instance, it occurs in three steps:
QDeclarativeComponent::beginCreate() differs from QDeclarativeComponent::create () in that it only performs step 1. QDeclarativeComponent::completeCreate () must be called to complete steps 2 and 3.
This breaking point is sometimes useful when using attached properties to communicate information to an instantiated component, as it allows their initial values to be configured before property bindings take effect.
[虚拟]
void
QDeclarativeComponent::
completeCreate
()
This method provides more advanced control over component instance creation. In general, programmers should use QDeclarativeComponent::create () to create a component.
Complete a component creation begin with QDeclarativeComponent::beginCreate ().
[虚拟]
QObject
* QDeclarativeComponent::
create
(
QDeclarativeContext
*
context
= 0)
Create an object instance from this component. Returns 0 if creation failed. context specifies the context within which to create the object instance.
若 context is 0 (the default), it will create the instance in the engine' s 根上下文 .
返回 QDeclarativeContext the component was created in. This is only valid for components created directly from QML.
Return the list of errors that occurred during the last compile or create operation. An empty list is returned if isError () is not set.
返回 true 若 status () == QDeclarativeComponent::Error .
返回 true 若 status () == QDeclarativeComponent::Loading .
返回 true 若 status () == QDeclarativeComponent::Null .
返回 true 若 status () == QDeclarativeComponent::Ready .
加载 QDeclarativeComponent from the provided url .
Ensure that the URL provided is full and correct, in particular, use QUrl::fromLocalFile () when loading a file from the local filesystem.
设置 QDeclarativeComponent to use the given QML data 。若 url is provided, it is used to set the component name and to provide a base path for items resolved by this component.