Modernización de los módulos QML
Los módulos QML se han vuelto más potentes y fáciles de usar en Qt 6. Las siguientes secciones describen cómo modernizar los módulos QML que ya utilizan qt_add_qml_module.
Consulte también Portar módulos QML a CMake sobre cómo portar un módulo QML a la API CMake qt_add_qml_module.
Usar qt_standard_project_setup
qt_standard_project_setup configura las políticas de Qt CMake necesarias para los módulos QML modernos, entre otras cosas. Para modernizar su módulo QML y seguir las mejores prácticas, llame a qt_standard_project_setup en el archivo CMakeLists.txt de nivel superior del proyecto antes de cualquier llamada a qt_add_qml_module:
qt_standard_project_setup(REQUIRES 6.8)
Utilice el nuevo prefijo de ruta de recursos estándar
La ruta de recursos estándar para los módulos QML pasó de :/ a :/qt/qml con QTP0001. No utilice prefijos de recursos personalizados ni amplíe las rutas de importación en el motor. Elimine todos los argumentos RESOURCE_PREFIX de todas las llamadas a qt_add_qml_module, así como todas las llamadas a QQmlEngine::addImportPath o similares. Cambie todas las rutas qrc en su código C++ y QML para utilizar el nuevo prefijo de ruta de recursos:
// C++ usages like:
QUrl someUrl("qrc:/MyQmlModule/MyResource1.png");
// need to be changed to
QUrl someUrl("qrc:/qt/qml/MyQmlModule/MyResource1.png");
// QML usages like:
":/MyQmlModule/MyResource1.png"
// need to be changed to
":/qt/qml/MyQmlModule/MyResource1.png"Véase también Uso del sistema de recursos Qt con QML.
Utilice loadFromModule para cargar sus archivos QML
Con la ruta de importación predeterminada, puede utilizar los métodos loadFromModule, como QQmlApplicationEngine::loadFromModule, QQuickView::loadFromModule, o QQmlComponent::loadFromModule, por ejemplo.
Utilice loadFromModule para cargar su archivo QML, por ejemplo:
engine.load(QUrl(QStringLiteral("qrc:/MyQmlModule/Main.qml")));
// becomes
engine.loadFromModule("MyQmlModule", "Main");Sustituya OUTPUT_DIRECTORY e IMPORT_PATH por DEPENDENCIES TARGET
Evite establecer un IMPORT_PATH en el qt_add_qml_module. En su lugar, utilice DEPENDENCIES TARGET para declarar dependencias de otros módulos QML que no se puedan encontrar en la ruta de importación actual.
El uso de DEPENDENCIES TARGET también elimina la necesidad de la variable CMake QT_QML_OUTPUT_DIRECTORY y el argumento OUTPUT_DIRECTORY para qt_add_qml_module, por lo que debe eliminar sus definiciones y usos.
Por ejemplo:
### in the CMakeLists.txt file defining the dependent QML module:
# don't set QT_QML_OUTPUT_DIRECTORY and remove lines like these:
set(QT_QML_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/qml)
qt_add_qml_module(MyThirdPartyQmlLibraryDependency
URI MyThirdPartyQmlLibraryDependency
....
# custom output paths are obsolete due to DEPENDENCIES TARGET below, so remove:
OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/qml
}
### in the CMakeLists.txt file defining the QML module that uses the dependency:
qt_add_qml_module(MyQmlLibrary
URI MyQmlModule
...
# replace import paths like these:
IMPORT_PATH ${CMAKE_CURRENT_BINARY_DIR}/thirdparty/qml
# with:
DEPENDENCIES TARGET MyThirdPartyQmlLibraryDependency
}Nota: Puede que necesite llamar a add_subdirectory() antes de llamar a qt_add_qml_module en su CMakeLists.txt para que DEPENDENCIES TARGET encuentre el objetivo.
Para obtener más información sobre cómo declarar dependencias de módulos, consulte Declarar dependencias de módulos.
Véase también Cambios en Qt Qml y Portar módulos QML a CMake.
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.