The QQmlIncubator class allows QML objects to be created asynchronously. 更多...
头: | #include <QQmlIncubator> |
qmake: | QT += qml |
enum | IncubationMode { Asynchronous, AsynchronousIfNested, Synchronous } |
enum | Status { Null, Ready, Loading, Error } |
QQmlIncubator (QQmlIncubator::IncubationMode mode = Asynchronous) | |
void | clear () |
QList<QQmlError> | errors () const |
void | forceCompletion () |
QQmlIncubator::IncubationMode | incubationMode () const |
bool | isError () const |
bool | isLoading () const |
bool | isNull () const |
bool | isReady () const |
QObject * | 对象 () const |
void | setInitialProperties (const QVariantMap & initialProperties ) |
QQmlIncubator::Status | status () const |
virtual void | setInitialState (QObject * object ) |
virtual void | statusChanged (QQmlIncubator::Status status ) |
Creating QML objects - like delegates in a view, or a new page in an application - can take a noticeable amount of time, especially on resource constrained mobile devices. When an application uses QQmlComponent::create () directly, the QML object instance is created synchronously which, depending on the complexity of the object, can cause noticeable pauses or stutters in the application.
The use of QQmlIncubator gives more control over the creation of a QML object, including allowing it to be created asynchronously using application idle time. The following example shows a simple use of QQmlIncubator.
QQmlIncubator incubator; component->create(incubator); while (!incubator.isReady()) { QCoreApplication::processEvents(QEventLoop::AllEvents, 50); } QObject *object = incubator.object();
Asynchronous incubators are controlled by a QQmlIncubationController that is set on the QQmlEngine , which lets the engine know when the application is idle and incubating objects should be processed. If an incubation controller is not set on the QQmlEngine , QQmlIncubator creates objects synchronously regardless of the specified IncubationMode .
QQmlIncubator supports three incubation modes:
The incubator will remain in the Loading state until either the creation is complete or an error occurs. The statusChanged () callback can be used to be notified of status changes.
Applications should use the Asynchronous incubation mode to create objects that are not needed immediately. For example, the ListView type uses Asynchronous incubation to create objects that are slightly off screen while the list is being scrolled. If, during asynchronous creation, the object is needed immediately the QQmlIncubator::forceCompletion () method can be called to complete the creation process synchronously.
In most scenarios where a QML component wants the appearance of a synchronous instantiation, it should use this mode.
This mode is best explained with an example. When the ListView type is first created, it needs to populate itself with an initial set of delegates to show. If the ListView was 400 pixels high, and each delegate was 100 pixels high, it would need to create four initial delegate instances. If the ListView used the Asynchronous incubation mode, the ListView would always be created empty and then, sometime later, the four initial items would appear.
Conversely, if the ListView was to use the Synchronous incubation mode it would behave correctly but it may introduce stutters into the application. As QML would have to stop and instantiate the ListView 's delegates synchronously, if the ListView was part of a QML component that was being instantiated asynchronously this would undo much of the benefit of asynchronous instantiation.
The AsynchronousIfNested mode reconciles this problem. By using AsynchronousIfNested , ListView delegates are instantiated asynchronously if the ListView itself is already part of an asynchronous instantiation, and synchronously otherwise. In the case of a nested asynchronous instantiation, the outer asynchronous instantiation will not complete until after all the nested instantiations have also completed. This ensures that by the time the outer asynchronous instantitation completes, inner items like ListView have already completed loading their initial delegates.
It is almost always incorrect to use the Synchronous incubation mode - elements or components that want the appearance of synchronous instantiation, but without the downsides of introducing freezes or stutters into the application, should use the AsynchronousIfNested incubation mode.
Specifies the mode the incubator operates in. Regardless of the incubation mode, a QQmlIncubator will behave synchronously if the QQmlEngine does not have a QQmlIncubationController set.
常量 | 值 | 描述 |
---|---|---|
QQmlIncubator::Asynchronous
|
0
|
The object will be created asynchronously. |
QQmlIncubator::AsynchronousIfNested
|
1
|
If the object is being created in a context that is already part of an asynchronous creation, this incubator will join that existing incubation and execute asynchronously. The existing incubation will not become Ready until both it and this incubation have completed. Otherwise, the incubation will execute synchronously. |
QQmlIncubator::Synchronous
|
2
|
The object will be created synchronously. |
Specifies the status of the QQmlIncubator .
常量 | 值 | 描述 |
---|---|---|
QQmlIncubator::Null
|
0
|
Incubation is not in progress. Call QQmlComponent::create () to begin incubating. |
QQmlIncubator::Ready
|
1
|
The object is fully created and can be accessed by calling 对象 (). |
QQmlIncubator::Loading
|
2
|
The object is in the process of being created. |
QQmlIncubator::Error
|
3
|
An error occurred. The errors can be access by calling errors (). |
Create a new incubator with the specified mode
Clears the incubator. Any in-progress incubation is aborted. If the incubator is in the Ready state, the created object is not deleted.
Return the list of errors encountered while incubating the object.
Force any in-progress incubation to finish synchronously. Once this call returns, the incubator will not be in the Loading state.
Return the incubation mode passed to the QQmlIncubator 构造函数。
Returns true if the incubator's status () is Error.
Returns true if the incubator's status () is Loading.
Returns true if the incubator's status () is Null.
Returns true if the incubator's status () is Ready.
Return the incubated object if the status is Ready, otherwise 0.
Stores a mapping from property names to initial values, contained in initialProperties , with which the incubated component will be initialized.
该函数在 Qt 5.15 引入。
另请参阅 QQmlComponent::setInitialProperties .
[virtual protected]
void
QQmlIncubator::
setInitialState
(
QObject
*
object
)
Called after the object is first created, but before property bindings are evaluated and, if applicable, QQmlParserStatus::componentComplete () is called. This is equivalent to the point between QQmlComponent::beginCreate () 和 QQmlComponent::completeCreate (), and can be used to assign initial values to the object's properties.
默认实现什么都不做。
Return the current status of the incubator.
[virtual protected]
void
QQmlIncubator::
statusChanged
(
QQmlIncubator::Status
status
)
Called when the status of the incubator changes. status is the new status.
默认实现什么都不做。