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:
The C++ side currently only consists of:
#include <QtQuickTest/quicktest.h> #include <QByteArray> static bool s_configEnv = qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard")); QUICK_TEST_MAIN(inputpanel)
(Yes, it is the test for the virtual keyboard ... g)
How do I access the engine here before the tests are run?
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.
- The provider() is called once the registered Singleton object is referred
- Then it will call a registered callback in another object
- 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 :)