import QtQuick.Controls 2.0 不工作 - QQmlApplicationEngine 加载组件失败

import QtQuick.Controls 2.0 not working - QQmlApplicationEngine failed to load component

我有一个应用程序,它的项目是使用 Qt5.7 中的 CMake 生成的, 因此,当 import QtQuick.Controls 2.0 应用程序加载失败并出现以下错误时:

plugin cannot be loaded for module "QtQuick.Controls": Cannot load library C:\Qt\Qt5.7.0.7\msvc2015\qml\QtQuick\Controls.2\qtquickcontrols2plugind.dll: The specified module could not be found.

CMakeLists.txt

set(CMAKE_PREFIX_PATH "C:\Qt\Qt5.7.0\5.7\msvc2015")
set(CMAKE_AUTOMOC ON) 
set(CMAKE_AUTORCC ON) 
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt5Core REQUIRED)
find_package(Qt5Qml) 
find_package(Qt5Quick) 
find_package(Qt5QuickControls2)

...

add_executable(MyApp ${SRC} ${HEADER} ${RESOURCES})

target_link_libraries(MyApp
Qt5::WinMain    
Qt5::Core   
Qt5::Qml    
Qt5::Quick  
Qt5::QuickControls2     
)

在visual studio中加载的DLL文件输出:

'MyApp.exe' (Win32): Loaded 'C:\Qt\Qt5.7.0.7\msvc2015\qml\QtQuick.2\qtquick2plugind.dll'. Symbols loaded.
'MyApp.exe' (Win32): Loaded 'C:\Qt\Qt5.7.0.7\msvc2015\qml\QtQuick\Controls.2\qtquickcontrols2plugind.dll'. Symbols loaded.
'MyApp.exe' (Win32): Unloaded 'C:\Qt\Qt5.7.0.7\msvc2015\qml\QtQuick\Controls.2\qtquickcontrols2plugind.dll'

我找到了解决方案,问题是 QtQuick.Controls 2.0 依赖于 QtQuick.Templates 2.0 模块,所以我将其 dll 复制到输出目录并成功运行。

所需的 DLL(对于调试版本):

Qt5QuickTemplates2d.dll
Qt5QuickControls2d.dll

所需的 DLL(发布版本):

Qt5QuickTemplates2.dll
Qt5QuickControls2.dll

On Windows Qt 提供了自动扫描所有 Qt 和 QML 依赖项的部署工具:

%QTDIR%\bin\windeployqt.exe your_app.exe --qmldir your\qml\files

See the Qt documentation:

The tool can be found in QTDIR/bin/windeployqt. It takes an .exe file or a directory that contains an .exe file as an argument, and scans the executable for dependencies. If a directory is passed with the --qmldir argument, windeployqt uses the qmlimportscanner tool to scan QML files inside the directory for QML import dependencies. Identified dependencies are then copied to the executable's directory. The hardcoded local paths in Qt5Core.dll are furthermore replaced with relative ones.

如果您使用的是 Ubunty,请尝试安装 qml-module-qtquick-controls2

sudo apt install qml-module-qtquick-controls2