Important: Please read the Qt Code of Conduct -

Access QML Engine in QtQuickTest

  • I need to do some initialization in the QmlEngine utilized by a QtQuickTest.

    The test is build on TestCase items, which need global properties that are usually set by something like this on the C++ side:

    myQuickView->engine()->rootContext()->setProperty(..., ...);

    The C++ side currently only consists of:

    #include <QtQuickTest/quicktest.h>
    #include <QByteArray>
    static bool s_configEnv = qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));

    (Yes, it is the test for the virtual keyboard ... g)

    How do I access the engine here before the tests are run?

    Kind regards.

  • Qt Champions 2016

    Probably it don't have a direct way to access QQmlEngine owned by QtQuickTest. I usually use a dirty hack. I would register a Singleton QObject and let it configure QQmlEngine during construction.


    testable/testrunnerwrapper.cpp at master · benlau/testable

    1. The provider() is called once the registered Singleton object is referred
    2. Then it will call a registered callback in another object
    3. It is a dirty hack. You must make sure the object is referred in the first test case. Otherwise, it may not be able to work as expected.

    However, in your case, provide the required data in a Singleton object would be much easier.

  • That did the trick, thank you :)

Log in to reply