Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Getting QML tests to reference QML in another directory
Qt 6.11 is out! See what's new in the release blog

Getting QML tests to reference QML in another directory

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
8 Posts 3 Posters 4.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DanCA-A
    wrote on last edited by DanCA-A
    #1

    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/qmltest.pro 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?

    Thanks,
    Dan

    A benlauB 2 Replies Last reply
    0
    • D DanCA-A

      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/qmltest.pro 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?

      Thanks,
      Dan

      A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

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

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DanCA-A
        wrote on last edited by
        #3

        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 qmltests.pro like this:
        RESOURCES += $$PWD/../app/qml.qrc

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

        <RCC>
            <qresource prefix="/">
                <file>main.qml</file>
                <file>Page1.qml</file>
                <file>Page1Form.ui.qml</file>
        ...
        

        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.

        Thanks,
        Dan

        A 1 Reply Last reply
        0
        • D DanCA-A

          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 qmltests.pro like this:
          RESOURCES += $$PWD/../app/qml.qrc

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

          <RCC>
              <qresource prefix="/">
                  <file>main.qml</file>
                  <file>Page1.qml</file>
                  <file>Page1Form.ui.qml</file>
          ...
          

          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.

          Thanks,
          Dan

          A Offline
          A Offline
          ambershark
          wrote on last edited by
          #4

          @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"));
          

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          1 Reply Last reply
          0
          • D DanCA-A

            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/qmltest.pro 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?

            Thanks,
            Dan

            benlauB Offline
            benlauB Offline
            benlau
            Qt Champions 2016
            wrote on last edited by
            #5

            @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?

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DanCA-A
              wrote on last edited by
              #6

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

              A 1 Reply Last reply
              0
              • D DanCA-A

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

                A Offline
                A Offline
                ambershark
                wrote on last edited by
                #7

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

                Trailing slashes are not necessary.

                My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                1 Reply Last reply
                1
                • D Offline
                  D Offline
                  DanCA-A
                  wrote on last edited by
                  #8

                  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,
                  Dan

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved