QQmlIncubationController Class
QQmlIncubationController 实例负责驱动 QQmlIncubators 的运行进程。更多内容...
| 标题: | #include <QQmlIncubationController> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Qml) target_link_libraries(mytarget PRIVATE Qt6::Qml) |
| qmake: | QT += qml |
公共函数
| QQmlIncubationController() | |
| QQmlEngine * | engine() const |
| void | incubateFor(int msecs) |
| void | incubateWhile(std::atomic<bool> *flag, int msecs = 0) |
| int | incubatingObjectCount() const |
受保护函数
| virtual void | incubatingObjectCountChanged(int incubatingObjectCount) |
详细说明
为了实现异步行为并避免应用程序出现卡顿或冻结,QQmlIncubators 的对象创建过程必须仅在应用程序的空闲时间进行。QQmlIncubationController 允许应用程序精确控制该处理何时发生、以何种频率进行以及持续多长时间。
应通过调用QQmlEngine::setIncubationController()方法,创建一个QQmlIncubationController的派生实例,并将其设置在QQmlEngine 上。随后,可根据应用程序的要求,通过调用QQmlIncubationController::incubateFor()或QQmlIncubationController::incubateWhile()方法来控制处理过程。
例如,以下是一个孵化控制器示例,它将在每 16 毫秒的时间间隔内进行最多 5 毫秒的孵化。
class PeriodicIncubationController : public QObject,
public QQmlIncubationController
{
public:
PeriodicIncubationController() {
startTimer(16);
}
protected:
void timerEvent(QTimerEvent *) override {
incubateFor(5);
}
};虽然该示例可以正常运行,但它经过了大幅简化。实际应用中的孵化控制器会尝试在不干扰应用程序的情况下,最大限度地利用空闲时间。如上例所示,使用固定的 5 毫秒时间,可能会导致某些帧中空闲时间被浪费,而在其他帧中又会干扰应用程序。
QQuickWindow QQuickView 和 均预先创建了孵化控制器,这些控制器采用更智能的算法,将孵化操作分散在多个帧中进行。您几乎无需自行编写此类控制器。QQuickWidget
成员函数文档
QQmlIncubationController::QQmlIncubationController()
创建一个新的孵化控制器。
QQmlEngine *QQmlIncubationController::engine() const
返回该孵化控制器所设置的QQmlEngine ,如果该控制器未设置在任何引擎上,则返回0。
void QQmlIncubationController::incubateFor(int msecs)
为msecs 孵化对象,或者直到没有更多对象需要孵化为止。
void QQmlIncubationController::incubateWhile(std::atomic<bool> *flag, int msecs = 0)
在flag 所指向的原子布尔值为true时对对象进行孵化,或者直到没有更多对象需要孵化为止,或者如果msecs 不为零,则孵化时间不超过msecs 。
通常,该方法与线程或 UNIX 信号配合使用,当需要中断孵化时,该线程或信号会将flag 所指向的布尔值设置为 false。
注意: 读取flag 时采用 acquire 内存排序。
int QQmlIncubationController::incubatingObjectCount() const
返回当前正在孵化的对象的数量。
[virtual protected] void QQmlIncubationController::incubatingObjectCountChanged(int incubatingObjectCount)
当孵化中的对象数量发生变化时调用此方法。incubatingObjectCount 表示孵化中的对象的新数量。
默认实现不执行任何操作。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.