8

I have an app that its project generated using CMake in Qt5.7, so when import QtQuick.Controls 2.0 application failed to load with the following error:

plugin cannot be loaded for module "QtQuick.Controls": Cannot load library C:\Qt\Qt5.7.0\5.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     
)

The DLL file loaded in visual studio output:

'MyApp.exe' (Win32): Loaded 'C:\Qt\Qt5.7.0\5.7\msvc2015\qml\QtQuick.2\qtquick2plugind.dll'. Symbols loaded.
'MyApp.exe' (Win32): Loaded 'C:\Qt\Qt5.7.0\5.7\msvc2015\qml\QtQuick\Controls.2\qtquickcontrols2plugind.dll'. Symbols loaded.
'MyApp.exe' (Win32): Unloaded 'C:\Qt\Qt5.7.0\5.7\msvc2015\qml\QtQuick\Controls.2\qtquickcontrols2plugind.dll'
Reza Ebrahimi
  • 3,623
  • 2
  • 27
  • 41
  • Which files do you have in `C:\Qt\Qt5.7.0\5.7\msvc2015\qml\QtQuick\Controls.2`? It looks like it's trying to find the debug version of the plugin, but does your Qt installation have it available? – jpnurmi Jun 25 '16 at 15:59
  • Yes, it the dll file is there in the path and loaded in visual studio (I see in the intellisense, its symbols loaded) but fails to load app. – Reza Ebrahimi Jun 25 '16 at 16:07
  • 1
    For some reason it unloads the plugin right away? I don't know what could cause that. What happens if you open the plugin with Dependency Walker? Is this a pre-built Qt installation from an installer? If so, does the Qt Quick Controls 2 Gallery example work? – jpnurmi Jun 25 '16 at 16:17
  • it unloads because of QQmlApplicationEngine failed to load component, so I'll use DependencyWalker, so the Gallery example works well when build and run from QtCreator. – Reza Ebrahimi Jun 25 '16 at 16:22

3 Answers3

10

I found the solution, The problem is QtQuick.Controls 2.0 depends on QtQuick.Templates 2.0 module so I have copied its dll to output directory and it runs successfully.

required DLLs (for Debug version):

Qt5QuickTemplates2d.dll
Qt5QuickControls2d.dll

required DLLs (for Release version):

Qt5QuickTemplates2.dll
Qt5QuickControls2.dll
Reza Ebrahimi
  • 3,623
  • 2
  • 27
  • 41
  • 1
    Is this something Qt's CMake integration can/should handle automatically? Is it normal that you have to list the whole dependency chain by hand? Or is it perhaps a missing dependency declaration somewhere in QQC2? – jpnurmi Jun 25 '16 at 17:48
  • Actually, I just noticed that QtGui and QtXmlPatterns are not explicitly listed either, so it must resolve dependencies to some degree. – jpnurmi Jun 25 '16 at 17:51
2

On Windows Qt provides deployment tool which automatically scans all Qt and QML dependencies:

%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.

tommyk
  • 3,187
  • 7
  • 39
  • 61
2

If you are using Ubunty try to install qml-module-qtquick-controls2

sudo apt install qml-module-qtquick-controls2
Andrey Semenov
  • 901
  • 11
  • 17