Skip to content

Commit

Permalink
Merge pull request qmlbook#184 from qmlbook/ch18-fix-plugin-install
Browse files Browse the repository at this point in the history
ch18: explain how to import modules
  • Loading branch information
e8johan authored May 30, 2022
2 parents 45bf675 + 2142da8 commit 51f40ff
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
25 changes: 20 additions & 5 deletions docs/ch18-extensions/create-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,25 @@ plugin fileio

The module is the URI that the user imports, and after it you name which plugin to load for the said URI. The plugin line must be identical with your plugin file name (under mac this would be *libfileio_debug.dylib* on the file system and *fileio* in the *qmldir*, for a Linux system, the same line would look for *libfileio.so*). These files are created by Qt Creator based on the given information.

The easier way to create a correct `qmldir` file is in the `CMakeLists.txt` for your project, in the `qt_add_qml_module` macro. Here, the `URI` parameter is used to specify the URI of the plugin, e.g. `org.example.io`. This way, the `qmldir` file is generated when the project is built.
The easier way to create a correct `qmldir` file is in the `CMakeLists.txt` for your project, in the `qt_add_qml_module` macro. Here, the `URI` parameter is used to specify the URI of the plugin, e.g. `org.example.io`. This way, the `qmldir` file is generated when the project is built. For the example here, the `qt_add_qml_module` macro looks as follows:

::: TODO
How to install the module?
:::
```
qt_add_qml_module(fileio PLUGIN_TARGET
VERSION 1.0.0
URI "org.example.io"
OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/imports/org/example/io/"
SOURCES
fileio.cpp
fileio.h
fileio_plugin.cpp
)
```

When importing a module called `org.example.io`, the QML engine will look in one of the import paths , e.g. the `QML2_IMPORT_PATH` environment variable, and try to locate the `org/example/io` path with a `qmldir` file. The `qmldir` file will tell the engine which library to load as a QML extension plugin using which module URI. Two modules with the same URI will override each other. For the example above, the module can be imported with the following command:

```
QML2_IMPORT_PATH=/home/.../ch18-extensions/src/fileio/imports \
./.../ch18-extensions/src/CityUI/CityUI
```

When importing a module called “org.example.io”, the QML engine will look in one of the import paths and tries to locate the org/example/iopath with a qmldir. The qmldir then will tell the engine which library to load as a QML extension plugin using which module URI. Two modules with the same URI will override each other.
Notice that the `QML2_IMPORT_PATH` points to the `imports` directory, and that the `org/example/io` sub-path is found via the `org.example.io` part of the import statement.
6 changes: 0 additions & 6 deletions docs/ch18-extensions/fileio-demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ The write method does the same but opens the file in write mode and uses the str
To make the type visible to QML, we add the `QML_ELEMENT` macro just after the `Q_PROPERTY` lines. This tells Qt that the type should be made available to QML. If you want to provide a different name than the C++ class, you can use the `QML_NAMED_ELEMENT` macro.
TODO TODO TODO
Do not forget to call `make install` at the end. Otherwise, your plugin files will not be copied over to the qml folder and the qml engine will not be able to locate the module.
TODO TODO TODO
::: tip
As the reading and writing are blocking function calls you should only use this `FileIO` for small texts, otherwise, you will block the UI thread of Qt. Be warned!
:::

0 comments on commit 51f40ff

Please sign in to comment.