Import on "." needed?

  • I've written a small test application using Qt Quick which runs on my main computer using qmlviewer.
    But when I use the "QML Application" wizard which generates a small application with a QDeclarativeView I always get strange errors. Even when copying the plain QML code to my netbook.

    @main.qml:22:5: LoginView is not a type
    LoginView {
    ^ @

    main.qml and LoginView.qml are in the same directory. Only a @import "."@ helps but I don't think this is the right way?!

    For information: Using Windows XP and Qt 4.7.0 (precompiled-msvc)

    On my main computer where it runs using qmlviewer:
    running "qmlviewer [path-to-qml-file]" leads to "LoginView is not a type"
    running "qmlviewer main.qml" from the same directory where main.qml is located works.

    Isn't qml expected to load from the paths where the current file is?

    It may be related to this "bug report": but it's marked as fixed for 4.7.0.

  • There is a section in the docs explaining how "QML Modules": import works and the two types of modules: location modules and installed modules.

    This doc section worth reading, summing up what happens is:

    • The current directory is by default part of the QML import path, that's why you don't need to import anything to use components defined in other files in the same directory;
    • You can import components from a directory, importing the quoted relative or absolute path to it;
    • Installed modules are imported using an URI relative to the path part of the QML_IMPORT_PATH variable.

    Here is an example showing the use of these types of modules.
    Consider the following tree:

    |_ main.qml
    |_ Test1.qml
    |_ modules
    |_ Test2.qml
    |_ installed_modules
    |_ qmldir
    |_ Test3.qml

    The Test*.qml files are QML components. Due to my lack of creativity, I used rectangles with different colors. The most important is in main.qml.

    • main.qml

    import Qt 4.7
    import "modules"
    import "" 1.0
    import installed_modules 1.0

    Rectangle {
    width: 400
    height: 300
    color: "green"

     Test1 {
        x: 30
        y: 30
     Test2 {
        x: 60
        y: 60
     Test3 {
        x: 100
        y: 80
     RemoteTest {
        x: 30
        y: 200


    • Test1 is in the same directory, so it's part of the QML import path.
    • Test2 is a location module, imported using the quoted relative path to its directory (Could be the absolute path).
    • Test3 is an installed module and the version is required in the import line.
    • RemoteTest is a location module as well, however it is not in the local filesystem.

    If you run qmlviewer main.qml there will be an error because qmlviewer won't find installed_modules because it is not in the QML import path. To fix that, add "/path/to/example/dir" to QML_IMPORT_PATH (On Windows you can set it in the environment variables dialog).

    Then, to solve:

    [quote author="gri" date="1286808499"]
    running "qmlviewer [path-to-qml-file]" leads to "LoginView is not a type"

    adjust the code and app structure according to one of the possible ways.

    PS: The example is available for download: "zip": or "tar.bz2": . The remote component is available too.

  • I forgot to mention one thing: qmlviewer has the -I option that also can be used to add paths to the QML import path variable.

    $ qmlviewer -I /path/to/example/dir main.qml

Log in to reply