Getting QML tests to reference QML in another directory

  • I'm trying to get QML tests to work, but I can't get any imports from the app directory to work.

    I have my application in panel/app/ (main.qml, Page1.qml, Page1Form.ui.qml)
    I have my tests in panel/qmltest/

    panel/qmltests/ looks like:

    TEMPLATE = app
    TARGET = tst_app
    CONFIG += warn_on qmltestcase
    SOURCES += tst_app.cpp
    IMPORTPATH += $$PWD/../app
    QML_IMPORT_PATH += $$PWD/../app

    panel/qmltests/tst_app.qml looks like:

    import QtQuick 2.3
    import QtTest 1.0
    TestCase {
        name: "MathTests"           /* just an example.  TODO test app pages*/
        Page1Form {
        function test_max_speed() {
            var got = max_left_speed()
            compare(got, 5000, "Max speed " + got + " != 5000")

    I run it like this:
    ../../build-gxrpanel-private_5_8-Debug/qmltests/tst_app -import "/home/dchristian/src/gxrpanel/app"

    And get:
    file:///home/dchristian/src/gxrpanel/qmltests/tst_app.qml:8:5: Page1Form is not a type
    Page1Form {
    It's not resolving the QML file.

    If I run it under strace, it's trying to open: /home/dchristian/src/gxrpanel/qmltests/Page1Form.qml
    Which implies that it's ignoring the import path.

    What's the right way to get a QML test to reference the app QML?


  • Moderators

    @DanCA-A I don't know if it will help but I put my qml files into a resource (qrc) for my project. Then as long as you set up your qrc properly and reference the files with a proper url they will all know about each other.

    As for your specific "on the filesystem" case I don't have any answers for you there.

  • That sounds interesting, but I'm not sure how to "set up your qrc properly and reference the files with a proper url ".

    I already have a qrc file in the app directory, so I tried referencing it by adding it to like this:
    RESOURCES += $$PWD/../app/qml.qrc

    The qrc file looks like this (all local relative references):

        <qresource prefix="/">

    It builds OK, but things still don't work. Same error as before.

    I don't know if how I imported it changed the paths. Is there a way to list the resource paths?

    I'm still referencing it as "Page1". Is that the right way to reference it?

    I'm new to Qt, so I may be missing something obvious.


  • Moderators

    @DanCA-A Your qrc is correct. The url needs to look like this excerpt from an app of mine:

    auto comp = new QQmlComponent(engine, QUrl("qrc:///main.qml"));

  • Qt Champions 2016

    @DanCA-A said in Getting QML tests to reference QML in another directory:

    ../../build-gxrpanel-private_5_8-Debug/qmltests/tst_app -import "/home/dchristian/src/gxrpanel/app"

    Missed a "/" in your import path? Is that /home/dchristian/src/gxr/panel/app?

  • Are you saying that a trailing / is needed for the import path?

  • Moderators

    @DanCA-A No he was suggesting you missed a / between gxr and panel.

    Trailing slashes are not necessary.

  • That just me being sloppy. The real name is gxrpanel, but I "cleaned it up" to panel; and then pasted in stuff with the full name.

    Apologies for the confusion,

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.