Qt5 Cmake project create qmldir
-
wrote on 25 Apr 2025, 12:46 last edited by
Hello, I am using qt5 in my project and I have made a cmake configuration. I keep more than one qml file in qrc in the project and I want to be able to call and import them as a module. For this, I use qmldir, but the module cannot be imported. Can you help me with this?
-
wrote 27 days ago last edited by
The QML files and the qmldir file must be placed inside a module folder, e.g., qml/MyModule/.
The qmldir file must be added to the .qrc resource file along with all the QML files it references.
The qmldir file must include:
A module declaration: module MyModule
A list of all QML types with version and filename:
Example: MyItem 1.0 MyItem.qmlIn C++, you must call
engine.addImportPath("qrc:/qml");
This tells the QML engine to look for modules inside the .qrc resource system.
In your qml.qrc, you must include all files in the correct relative path:
<file>qml/MyModule/qmldir</file> <file>qml/MyModule/MyItem.qml</file>
Use the module in QML with the correct name and version, as defined in qmldir:
import MyModule 1.0
Filenames in qmldir must match exactly (case-sensitive) with the actual file names.
CMake must include the .qrc file using qt5_add_resources() and link against Qt5::Qml and Qt5::Quick.
1/2