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();
}
}
参数: |
|
---|
参数: | arg__1 – PySide.QtDeclarative.QDeclarativeContext |
---|---|
返回类型: | PySide.QtCore.QObject |
参数: | context – PySide.QtDeclarative.QDeclarativeContext |
---|---|
返回类型: | PySide.QtCore.QObject |
参数: |
|
---|---|
返回类型: |
参数: | parent – PySide.QtCore.QObject |
---|---|
返回类型: | PySide.QtScript.QScriptValue |
返回类型: | PySide.QtDeclarative.QDeclarativeContext |
---|
返回类型: | unicode |
---|
返回类型: |
---|
返回类型: | PySide.QtCore.bool |
---|
返回类型: | PySide.QtCore.bool |
---|
返回类型: | PySide.QtCore.bool |
---|
返回类型: | PySide.QtCore.bool |
---|
参数: | url – PySide.QtCore.QUrl |
---|
返回类型: | PySide.QtCore.qreal |
---|
参数: | arg__1 – PySide.QtCore.qreal |
---|
参数: |
|
---|
返回类型: | PySide.QtDeclarative.QDeclarativeComponent.Status |
---|
参数: | arg__1 – PySide.QtDeclarative.QDeclarativeComponent.Status |
---|
返回类型: | PySide.QtCore.QUrl |
---|