Details
-
Suggestion
-
Resolution: Done
-
P2: Important
-
None
-
None
-
c4c75c8f3 (dev), fd0d36dbf (6.5), 2d8b15cc1 (dev), aa8d19a75 (dev), 71c281b9e (6.5.0), 90852fb3a (6.5), ad3f765a0 (6.5), 5340581f9 (tqtc/lts-6.2), 528d34d07 (6.5.0), e905d82d5 (dev), 6e713cb90 (6.9), c6297e2a3 (6.8), 85f5d11f4 (tqtc/lts-6.5)
Description
Consider the example at https://doc.qt.io/qt-6/cmake-build-reusable-qml-module.html
Project structure:
├── CMakeLists.txt └── example └── mylib ├── CMakeLists.txt ├── mytype.cpp ├── mytype.h
Subdirectory's CMakeLists.txt contains:
qt6_add_qml_module(mylib URI example.mylib VERSION 1.0 SOURCES mytype.h mytype.cpp )
If we follow this example to implement a minimal QML module, then the path to the module's qmldir file will be ":/example/mylib/qmldir"
Things that are not clear and can cause confusion
import example.mylib will normally fail to find the module, unless we call engine.addImportPath(":/") first. To add to the confusion, omitting engine.addImportPath(":/") can seem to work just fine on the development machine but fail mysteriously when deployed. This is because CMake puts a copy of the QML module in the same build folder as the executable, allowing the QML engine to import the module from qApp->applicationDirPath() (which is a default import path).
Furthermore, the relationships between URI, RESOURCE_PREFIX, and import path is not clearly documented anywhere. Some aspects are described at https://doc.qt.io/qt-6/qt-add-qml-module.html#resource-prefix but this section is only really comprehensible to someone who already has a decent understanding of the QML engine.
Suggestions (Updated according to comments in January)
Add the resource root directory (":/") as a default QQmlEngine import path so that users don't need to add it manuallyDocument that the default RESOURCE_PREFIX is / at https://doc.qt.io/qt-6/qt-add-qml-module.html#resource-prefix- Doc: Update the following pages to promote
AUTO_RESOURCE_PREFIX:(that becameQTP001)- [-https://doc-snapshots.qt.io/qt6-dev/cmake-build-reusable-qml-module.html-]
- https://doc-snapshots.qt.io/qt6-dev/qtquick-bestpractices.html
Doc: Add a simple, beginner-friendly section to https://doc.qt.io/qt-6/cmake-build-qml-application.html which describes how the example URI results in the final resource path
Qt Creator: The "New Project" wizard should add "6.5" as a possible "Minimum required Qt version". If 6.5 is selected, then the generated template should contain AUTO_RESOURCE_PREFIX
Attachments
Issue Links
- split to
-
QTBUG-133873 Provide comprehensive documentation for imports in QML
-
- Reported
-
For Gerrit Dashboard: QTBUG-109799 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
461770,4 | Update Building a QML application for 6.5 | dev | qt/qtdoc | Status: MERGED | +2 | 0 |
462631,2 | Update Building a QML application for 6.5 | 6.5 | qt/qtdoc | Status: MERGED | +2 | 0 |
463866,2 | QQmlEngine: Improve import path method documentation | dev | qt/qtdeclarative | Status: MERGED | +2 | 0 |
463871,2 | Import path: Explicitly recommend "qrc:/qt/qml" | dev | qt/qtdeclarative | Status: MERGED | +2 | 0 |
464046,2 | QQmlEngine: Improve import path method documentation | 6.5 | qt/qtdeclarative | Status: MERGED | +2 | 0 |
464047,2 | QQmlEngine: Improve import path method documentation | 6.5.0 | qt/qtdeclarative | Status: MERGED | +2 | 0 |
464048,2 | Import path: Explicitly recommend "qrc:/qt/qml" | 6.5 | qt/qtdeclarative | Status: MERGED | +2 | 0 |
464049,2 | Import path: Explicitly recommend "qrc:/qt/qml" | 6.5.0 | qt/qtdeclarative | Status: MERGED | +2 | 0 |
464056,2 | QQmlEngine: Improve import path method documentation | tqtc/lts-6.2 | qt/tqtc-qtdeclarative | Status: MERGED | +2 | 0 |
625616,3 | doc: Mention import path in "Building a QML application" | dev | qt/qtdoc | Status: MERGED | +2 | 0 |
627575,2 | doc: Mention import path in "Building a QML application" | 6.9 | qt/qtdoc | Status: MERGED | +2 | 0 |
627634,2 | doc: Mention import path in "Building a QML application" | 6.8 | qt/qtdoc | Status: MERGED | +2 | 0 |
627726,2 | doc: Mention import path in "Building a QML application" | tqtc/lts-6.5 | qt/tqtc-qtdoc | Status: MERGED | +2 | 0 |