October 11, 2010

gri gri
Lab Rat
36 posts

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.

  1. main.qml:22:5: LoginView is not a type
  2.          LoginView {
  3.          ^

main.qml and LoginView.qml are in the same directory. Only a

  1. import "."
helps but I don’t think this is the right way?!

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

Edit:
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 [bugreports.qt.nokia.com] but it’s marked as fixed for 4.7.0.

2 replies

October 17, 2010

anselmolsm anselmolsm
Ant Farmer
384 posts

There is a section in the docs explaining how QML Modules [doc.qt.nokia.com] 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:

  1. /path/to/example/dir
  2.    |_ main.qml
  3.    |_ Test1.qml
  4.    |_ modules
  5.           |_ Test2.qml
  6.    |_ installed_modules
  7.           |_ qmldir
  8.           |_ 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

  1. import Qt 4.7
  2. import "modules"
  3. import "http://www.anselmolsm.org/public/qt/qml/remote_example" 1.0
  4. import installed_modules 1.0
  5.  
  6. Rectangle {
  7.      width: 400
  8.      height: 300
  9.      color: "green"
  10.  
  11.      Test1 {
  12.         x: 30
  13.         y: 30
  14.      }
  15.  
  16.      Test2 {
  17.         x: 60
  18.         y: 60
  19.      }
  20.  
  21.      Test3 {
  22.         x: 100
  23.         y: 80
  24.      }
  25.  
  26.      RemoteTest {
  27.         x: 30
  28.         y: 200
  29.      }
  30. }

  • 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:

gri wrote:
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 [anselmolsm.org] or tar.bz2 [anselmolsm.org] . The remote component is available too.

 Signature 

Anselmo L. S. Melo (anselmolsm)
www.anselmolsm.org

October 18, 2010

anselmolsm anselmolsm
Ant Farmer
384 posts

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.

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

 Signature 

Anselmo L. S. Melo (anselmolsm)
www.anselmolsm.org

 
  ‹‹ [Solved] Where does openDatabaseSync() come from?      ActiveQt Com and QEvents searching for help! ››

You must log in to post a reply. Not a member yet? Register here!