QQuickTest Namespace

QQuickTest 命名空间包含与Qt Quick Test 相关的所有函数和宏。更多

Header: #include <QtQuickTest>

函数

bool qIsPolishScheduled(const QQuickItem *item)
(since 6.4) bool qIsPolishScheduled(const QQuickWindow *window)
(since 6.4) bool qWaitForPolish(const QQuickItem *item, int timeout = defaultTimeout)
(since 6.4) bool qWaitForPolish(const QQuickWindow *window, int timeout = defaultTimeout)

QUICK_TEST_MAIN(name)
QUICK_TEST_MAIN_WITH_SETUP(name, QuickTestSetupClass)

详细说明

有关如何编写Qt Quick 单元测试的信息,请参见 Qt Quick Test 简介

要链接到Qt Quick Test C++ 库,请参见 Qt Quick Test C++ API.

另请参阅 QML 测试前执行 C++

函数文档

bool QQuickTest::qIsPolishScheduled(const QQuickItem *item)

如果自上次调用polish() 以来,updatePolish() 未在item 上被调用,则返回true ,否则返回false

在 QML 中为属性赋值时,因赋值而必须对项进行的布局处理可能不会立即生效,而是会推迟到项被打磨后才生效。在这种情况下,您可以使用此函数来确保在继续执行测试之前,项目已被完善。例如

QVERIFY(QQuickTest::qIsPolishScheduled(item));
QVERIFY(QQuickTest::qWaitForItemPolished(item));

如果没有调用qIsPolishScheduled() ,调用qWaitForItemPolished() 可能会发现没有安排抛光,因此会立即通过,并假定项目已经抛光。这个函数让人一目了然项目没有被打磨的原因,并允许测试在这种情况下提前失败。

该函数的 QML 对应函数是isPolishScheduled() 。

另请参阅 QQuickItem::polish() 和QQuickItem::updatePolish()。

[since 6.4] bool QQuickTest::qIsPolishScheduled(const QQuickWindow *window)

此函数重载 qIsPolishScheduled()。

如果此窗口管理的任何项目中存在qIsPolishScheduled(item) ,则返回true ,否则返回true ,否则返回false

例如,如果场景中的某个项目可能已抛光,也可能未抛光,但如果已抛光,您需要等待它,您可以使用下面的代码:

if (QQuickTest::qIsPolishScheduled(window))
    QVERIFY(QQuickTest::qWaitForPolish(window));

此函数的 QML 对应函数是isPolishScheduled()。

此函数在 Qt 6.4 中引入。

另请参阅 QQuickItem::polish()、QQuickItem::updatePolish() 和QQuickTest::qWaitForPolish()。

[since 6.4] bool QQuickTest::qWaitForPolish(const QQuickItem *item, int timeout = defaultTimeout)

等待timeout 毫秒或直到item 上的updatePolish() 被调用。

如果在timeout 毫秒内调用了item 上的updatePolish() ,则返回true ,否则返回false

此函数在 Qt 6.4 中引入。

另请参阅 QQuickItem::polish()、QQuickItem::updatePolish() 和QQuickTest::qIsPolishScheduled()。

[since 6.4] bool QQuickTest::qWaitForPolish(const QQuickWindow *window, int timeout = defaultTimeout)

对于window 管理的所有项目,等待timeout 毫秒或qIsPolishScheduled(item) 返回false

如果qIsPolishScheduled(item)timeout 毫秒内对所有项目返回 false,则返回true ,否则返回false

此函数的 QML 对应函数是waitForPolish()。

此函数在 Qt 6.4 中引入。

另请参阅 QQuickItem::polish()、QQuickItem::updatePolish() 和QQuickTest::qIsPolishScheduled()。

宏文档

QUICK_TEST_MAIN(name)

Qt Quick Test 应用程序设置入口点。name 参数是这组测试的唯一标识。

#include <QtQuickTest>
QUICK_TEST_MAIN(example)

注意: 除非设置了QUICK_TEST_SOURCE_DIR 环境变量,否则宏假定测试源位于当前目录下。

另请参阅 QUICK_TEST_MAIN_WITH_SETUP() 和运行Qt Quick 测试

QUICK_TEST_MAIN_WITH_SETUP(name, QuickTestSetupClass)

Qt Quick Test 应用程序设置入口点。name 参数唯一标识了这组测试。

这个宏与QUICK_TEST_MAIN() 完全相同,只是它多了一个参数QuickTestSetupClass ,即QObject 派生类的类型,该类将被实例化。有了这个类,就可以定义额外的设置代码,在运行 QML 测试前执行。

注意: 除非设置了QUICK_TEST_SOURCE_DIR 环境变量,否则宏假定测试源在当前目录下。

下面的代码段演示了该宏的使用:

// src_qmltest_qquicktest.cpp
#include <QtQuickTest>
#include <QQmlEngine>
#include <QQmlContext>
#include <QGuiApplication>

class Setup : public QObject
{
    Q_OBJECT

public:
    Setup() {}

public slots:
    void applicationAvailable()
    {
        // Initialization that only requires the QGuiApplication object to be available
    }

    void qmlEngineAvailable(QQmlEngine *engine)
    {
        // Initialization requiring the QQmlEngine to be constructed
        engine->rootContext()->setContextProperty("myContextProperty", QVariant(true));
    }

    void cleanupTestCase()
    {
        // Implement custom resource cleanup
    }
};

QUICK_TEST_MAIN_WITH_SETUP(mytest, Setup)

#include "src_qmltest_qquicktest.moc"

另请 QUICK_TEST_MAIN() 和 RunningQt Quick Tests(运行 测试)。

© 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.