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() 方法在QQmlEngine 上创建和设置一个 QQmlIncubationController 派生实例。然后,根据应用程序的要求,通过调用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 指向的原子 bool 为真时孵化对象,或者直到没有更多对象需要孵化,或者如果msecs 不为零,则孵化到msecs 。
一般情况下,该方法与线程或 UNIX 信号结合使用,当线程或 UNIX 信号希望中断孵化时,会将flag 指向的 bool 设置为 false。
注意: flag 采用获取内存排序方式读取。
int QQmlIncubationController::incubatingObjectCount() const
返回当前孵化对象的数量。
[virtual protected]
void QQmlIncubationController::incubatingObjectCountChanged(int incubatingObjectCount)
当孵化对象数量发生变化时调用。incubatingObjectCount 是新的孵化对象数量。
默认情况下不做任何操作。
© 2025 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.