The PySide.QtDeclarative.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 PySide.QtDeclarative.QDeclarativeComponent instance can be created from a QML file. For example, if there is a main.qml file like this:
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 PySide.QtDeclarative.QDeclarativeComponent.create() , and then queries the Item ‘s width value:
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 PySide.QtDeclarative.QDeclarativeComponent is a network resource, or if the QML document references a network resource, the PySide.QtDeclarative.QDeclarativeComponent has to fetch the network data before it is able to create objects. In this case, the PySide.QtDeclarative.QDeclarativeComponent will have a Loading PySide.QtDeclarative.QDeclarativeComponent.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 PySide.QtDeclarative.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(); } }
Parameters: |
|
---|
Parameters: | arg__1 – PySide.QtDeclarative.QDeclarativeContext |
---|---|
Return type: | PySide.QtCore.QObject |
Parameters: | context – PySide.QtDeclarative.QDeclarativeContext |
---|---|
Return type: | PySide.QtCore.QObject |
Parameters: | parent – PySide.QtCore.QObject |
---|---|
Return type: | PySide.QtScript.QScriptValue |
Return type: | PySide.QtDeclarative.QDeclarativeContext |
---|
Return type: | unicode |
---|
Return type: |
---|
Return type: | PySide.QtCore.bool |
---|
Return type: | PySide.QtCore.bool |
---|
Return type: | PySide.QtCore.bool |
---|
Return type: | PySide.QtCore.bool |
---|
Parameters: | url – PySide.QtCore.QUrl |
---|
Return type: | PySide.QtCore.qreal |
---|
Parameters: | arg__1 – PySide.QtCore.qreal |
---|
Parameters: |
|
---|
Return type: | PySide.QtDeclarative.QDeclarativeComponent.Status |
---|
Parameters: | arg__1 – PySide.QtDeclarative.QDeclarativeComponent.Status |
---|
Return type: | PySide.QtCore.QUrl |
---|