Building Qt Qml Plugin using CMake - qt

I want to compile Qt Qml Plugin using CMake (instead of QMake) and add this plugin to the some application. Plugin and application script should be in separate CMakeLists.txt.
File structure:
CMakeLists.txt
main.cpp
main.qml
qml.qrc
plugin/
CMakeLists.txt
MyItem.cpp
MyItem.h
Plugin.h
qmldir
I have a script plugin/CMakeLists.txt that generates libmyplugin.dylib and qmldir and put it to the plugin/MyPlugin subfolder of plugin binary dir:
plugin/CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
add_library(myplugin SHARED
Item.cpp
Item.h
Plugin.h
)
set_target_properties(myplugin PROPERTIES
AUTOMOC TRUE
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/package/MyPlugin
)
# Copy qmldir file
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/package/MyPlugin/qmldir
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/qmldir
COMMAND "${CMAKE_COMMAND}" -E copy "${CMAKE_CURRENT_SOURCE_DIR}/qmldir" "${CMAKE_CURRENT_BINARY_DIR}/package/MyPlugin/qmldir"
COMMENT "Copy qmldir"
)
target_sources(myplugin PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/package/MyPlugin/qmldir
)
find_package(Qt5 REQUIRED COMPONENTS Core Qml Quick)
target_link_libraries(viewWorld PUBLIC Qt5::Core Qt5::Qml Qt5::Quick)
Now I need to build myplugin target before/after app target and copy plugin files from <myplugin binary dir>/plugin into location of app target binary (in case of Windows and Linux) or into <app bundle>/Contents/PlugIns (in case of macOS):
CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
add_executable(app
main.cpp
qml.qrc
)
set_target_properties(app PROPERTIES
AUTORCC TRUE
)
add_subdirectory(plugin)
add_dependencies(app plugin)
# Needs to copy plugin files...
find_package(Qt5 REQUIRED COMPONENTS Core Qml Quick)
target_link_libraries(viewWorld PUBLIC Qt5::Core Qt5::Qml Qt5::Quick)
I need to copy plugin files only if they changed. It would be great to write function that will take app target and plugin target and create appropriate dependencies.

For my Qt projects and qml plugins I using next rules. It's actual for win platform, but I hope it will be helpfull for your macOS
Output directory is same for all projects:
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
So all sub_directories with CMakeLists and it's project targets will build into same directory. It's helpfull for project running and finnaly deploying.
Qml plugins I copy to directory
set_target_properties(targetProject PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>/plugins/${QmlPluginName}/)
After build qml plugin I'm copy qmldir file to RUNTIME_OUTPUT_DIRECTORY
add_custom_command(TARGET ${targetProject} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
${QML_DIR_FILE}
${CMAKE_BINARY_DIR}/bin/$<CONFIG>/plugins/${QmlPluginName}/qmldir)
After this steps I had hext directory structure:
/build_dir/bin/Debug/
|application.exe
|plugins/
| |QmlPluginName/
| | |qmldir
| | |QmlPluginName.dll
So after this steps don't forget about:
QQmlApplicationEngine engine;
engine.addImportPath("./plugins");
P.S. sorry for my English

Related

CMake Qt UIC failed

I'm currently moving my project from QMake to CMake and I'm facing a problem with Qt UIC which try to process an ui file that does not exist instead of the actual file I want him to process.
I have the following architecture
.
|___ CMakeLists.txt
|___ MyProject.pro
|___ mainwindow.ui
|___ resource.qrc
|___ source
| |___ mainwindow.cpp
| |___ *.cpp
|___ include
| |___ mainwindow.h
| |___ *.h
And here is my cmake
cmake_minimum_required(VERSION 3.2)
# Project name
project(project)
# Tell CMake to compile with C++11
set(CMAKE_CXX_STANDARD 11)
# Tell CMake to run moc when needed.
set(CMAKE_AUTOMOC ON)
# Tell CMake to run uic when needed.
set(CMAKE_AUTOUIC ON)
# Tell CMake to run rcc when needed
set(CMAKE_AUTORCC ON)
# Moc generated files are located in the current dir so we need to tell CMake to look for them.
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Find Qt5
find_package(Qt5 COMPONENTS Widgets Core Gui OpenGL REQUIRED)
# Add Qt5 definitions and includes to build libraries.
# Widgets add Widgets Core and Gui
add_definitions(${Qt5Widgets_DEFINITIONS})
include_directories(${Qt5Widgets_INCLUDES})
add_definitions(${Qt5OpenGL_DEFINITIONS})
include_directories(${Qt5OpenGL_INCLUDES})
# Find OpenGL
find_package(OpenGL REQUIRED)
# Set include directories
include_directories(${CMAKE_SOURCE_DIR}/include
# Use Qt5 ressources
set(RESOURCE resources.qrc)
# Use Qt5 ui
set(UI mainwindow.ui)
# Adding sources
set(SOURCES
source/main.cpp
source/mainwindow.cpp
source/octree.cpp
source/mesh.cpp
source/pgm3d.cpp
source/glwidget.cpp
source/camera.cpp
source/scene.cpp
source/light.cpp
source/obj.cpp
source/alignedbox3f.cpp
source/wireboundingbox.cpp
include/mainwindow.h
include/utils.h
include/octree.h
include/mesh.h
include/pgm3d.h
include/glwidget.h
include/camera.h
include/scene.h
include/model3d.h
include/light.h
include/obj.h
include/alignedbox3f.h
include/wireboundingbox.h)
add_executable(${PROJECT_NAME} ${UI} ${RESOURCE} ${SOURCES} )
target_link_libraries(${PROJECT_NAME} ${Qt5Widgets_LIBRARIES} ${Qt5OpenGL_LIBRARIES} ${OPENGL_LIBRARIES})
And I've got the following error:
File '/*/project/source/mainwindow.ui' is not valid
AUTOUIC: error: process for ui_mainwindow.h needed by
"/*/project/source/mainwindow.cpp"
failed:
File '/*/project/source/mainwindow.ui' is not valid
This error is perfectly logical since my source folder does not contain the ui file. I've tried to wrap the ui instead of using autouic and it didn't work either.
The AUTOUIC property of the target (which is set from the variable CMAKE_AUTOUIC when the target is created) defines behavior of generator - when AUTOUIC property is enabled CMake will scan source files for the includes of ui files (like #include "ui_*.h" or #include <ui_*.h>) and will automatically process them with uic tool.
In CMake 3.9 another target property was added AUTOUIC_SEARCH_PATHS. Similar question was already asked here.
Another option for you would be to disable AUTOUIC and use qt5_wrap_ui with full path to the UI file:
set(CMAKE_AUTOUIC OFF)
set(UI ${CMAKE_CURRENT_LIST_DIR}/mainwindow.ui)
...
qt5_wrap_ui(UI_HEADERS ${UI})
add_executable(${PROJECT_NAME} ${UI_HEADERS} ${RESOURCE} ${SOURCES} )

Use a precompiled plugin (via a shared library) in a QML application

I have a QML plugin compiled (to a .so) by another project. I want to re-use this in my own QML application without re-building it each time. I want to copy the .so over and, with minimal additional code, be able to write:
import QQuickMapboxGL 1.0
at the top of my QML files and have it work.
Where do I need to copy the .so in my QML project, and how do I need to add it to the project so that the QML runtime can find it?
What I've tried:
Create a QQuickMapboxGL directory with libqmapboxgl.so in it.
Create a qmldir file in that directory with the contents:
plugin qmapboxgl
Add the following to my .pro file:
INSTALL_DIR = $$PWD/../install
target.path = $$INSTALL_DIR
# Copy the QQuickMapboxGL folder to the install directory
plugin.files = QQuickMapboxGL/*
plugin.path = $$INSTALL_DIR/QQuickMapboxGL
INSTALLS += target plugin
Add a make install build step.
The result of this mad hackery was:
plugin cannot be loaded for module "QQuickMapboxGL": Plugin verification data mismatch in '/my/build/QQuickMapboxGL/libqmapboxgl.so'
I have verified that the plugin and my application are both being compiled with the same version of g++ (g++-5 (Ubuntu 5.4.1-2ubuntu1~14.04) 5.4.1 20160904) and the same Qt download (5.7.0).
The main problem is that the .so is not a QML Plugin; no class inherits from QQmlExtensionPlugin or related. It is just a shared library of code.
Was able to workaround this by:
Adding the header files for MapboxGL to my project
In my main.cpp:
#include "3rdparty/mapbox-gl-native/platform/qt/include/qmapbox.hpp"
Calling QMapbox::registerTypes(); (inside main)
Copying libmapboxgl.so (built via Mapbox's make/cmake) inside a libs directory.
In myproject.pro adding: LIBS += -L./libs -lqmapboxgl
In my QML code import QQuickMapboxGL 1.0 and then using MapboxMap
Copying libmapboxgl.so to somewhere that is referenced by LD_LIBRARY_PATH

Parent CMakeLists.txt overwriting child CMakeLists.txt output directory options

I am trying to place a library from a project into a certain directory in my build output, but the parent CMakeLists.txt is overwriting the output settings.
The parent CMakeLists.txt sets all libraries to be placed in a /lib directory.
One of my libraries, however, needs to be placed into a /python library. The settings I have work on Windows. Meaning, all libs excluding my python specific library get placed in a /lib folder, and the python lib gets placed into the /python folder.
The problem appears when I build on Linux. All of the libraries, including the python specific library, get placed into the /lib folder. The FORCE option does nothing.
If I were only building for one platform, I could deal with either directory layout. But I really want to preserve the same layout across platforms.
CMakeLists.txt as follows:
-Parent CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(renderer2d)
#enable debug symbols by default
if(CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE Debug)
endif()
#(you can also set on cl: -D CMAKE_BUILD_TYPE=Release)
#place outside of Debug/Release folders
SET(OUTPUT_BINDIR ${PROJECT_BINARY_DIR}/bin)
MAKE_DIRECTORY(${OUTPUT_BINDIR})
SET(OUTPUT_LIBDIR ${PROJECT_BINARY_DIR}/lib)
MAKE_DIRECTORY(${OUTPUT_LIBDIR})
SET (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_LIBDIR} CACHE PATH "build directory")
SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_BINDIR} CACHE PATH "build directory")
IF(WIN32)
SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_BINDIR} CACHE PATH "build directory")
ELSE(WIN32)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_LIBDIR} CACHE PATH "build directory")
ENDIF(WIN32)
# For each configuration (Debug, Release, MinSizeRel... and/or anything the user chooses)
FOREACH(CONF ${CMAKE_CONFIGURATION_TYPES})
# Go uppercase (DEBUG, RELEASE...)
STRING(TOUPPER "${CONF}" CONF)
SET("CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_LIBDIR}")
SET("CMAKE_RUNTIME_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_BINDIR}")
IF(WIN32)
SET("CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_BINDIR}")
ELSE()
SET("CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_LIBDIR}")
ENDIF()
ENDFOREACH()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
#set the source directory
file(GLOB SOURCES src/*.cpp)
add_subdirectory(shape)
add_subdirectory(py_shape)
add_subdirectory(scripts)
#define sources and executable
set(EXECUTABLE_NAME "renderer2d")
add_executable(${EXECUTABLE_NAME} ${SOURCES})
#find python
find_package(PythonInterp)
find_package(PythonLibs 2.7 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
#detect and add SFML
#this line checks a cmake file for hints on where to find cmake
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
#find any version 2.x of SFML
#see the FindSFML.cmake file for additional details and instructions
find_package(SFML 2 REQUIRED system window graphics network audio)
include_directories(${SFML_INCLUDE_DIR})
#find and include Boost python libraries
set(Boost_USE_STATIC_LIBS OFF)
find_package(Boost COMPONENTS python system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
#link all found libraries to the executable
if(WIN32)
target_compile_definitions(${EXECUTABLE_NAME} PRIVATE $<$<BOOL:${MSVC}>:BOOST_ALL_NO_LIB>)
endif(WIN32)
target_link_libraries(${EXECUTABLE_NAME} ${PYTHON_LIBRARIES} ${SFML_LIBRARIES} ${Boost_LIBRARIES} shape)
-Child CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(py_shape CXX)
#set file variables
file(GLOB SOURCE src/*.cpp)
file(GLOB HEADERS inc/*.hpp)
#place outside of Debug/Release folders
SET(OUTPUT_BINDIR ${CMAKE_BINARY_DIR}/python)
MAKE_DIRECTORY(${OUTPUT_BINDIR})
set(OUTPUT_LIBDIR ${CMAKEK_BINARY_DIR}/python)
MAKE_DIRECTORY(${OUTPUT_LIBDIR})
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_LIBDIR} CACHE PATH "build directory")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_BINDIR} CACHE PATH "build directory")
IF(WIN32)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_BINDIR} CACHE PATH "build directory")
ELSE(WIN32)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_LIBDIR} CACHE PATH "build directory")
ENDIF(WIN32)
#for each configuration
FOREACH(CONF ${CMAKE_CONFIGURATION_TYPES})
#Go uppercase {DEBUG, RELEASE...)
SET("CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_LIBDIR}")
SET("CMAKE_RUNTIME_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_BINDIR}")
IF(WIN32)
SET("CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_BINDIR}")
ELSE()
SET("CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_LIBDIR}")
ENDIF()
ENDFOREACH()
#find packages
find_package(PythonInterp)
find_package(PythonLibs 2.7 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
find_package(Boost COMPONENTS python REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML 2 REQUIRED system window graphics network audio)
include_directories(${SFML_INCLUDE_DIR})
#build the library
add_library(python_shape MODULE ${SOURCE})
#enable C++11 if available
target_compile_features(python_shape PRIVATE cxx_range_for)
#link library
target_link_libraries(python_shape shape ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} ${SFML_LIBRARIES})
#drop "lib" from the library name
set_target_properties(python_shape PROPERTIES PREFIX "")
if(WIN32)
#set extension to ".pyd"
set_target_properties(python_shape PROPERTIES SUFFIX ".pyd")
endif(WIN32)
Turning my comments into an answer
I successfully tested the following with MinGW and CMake 3.3.0 (I reduced your example a little to concentrate on the output directories):
CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(renderer2d C CXX)
# TODO: Remove, this is just for testing
file(WRITE "src/renderer2d.cpp" "int main(void) {}")
file(WRITE "py_shape/src/py_shape.cpp" "")
#enable debug symbols by default
if(CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE Debug)
endif()
#(you can also set on cl: -D CMAKE_BUILD_TYPE=Release)
#place outside of Debug/Release folders
#see http://www.cmake.org/Wiki/CMake_Useful_Variables
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_BINARY_DIR}/bin")
if(WIN32)
set(LIBRARY_OUTPUT_PATH "${EXECUTABLE_OUTPUT_PATH}")
else()
set(LIBRARY_OUTPUT_PATH "${PROJECT_BINARY_DIR}/lib")
endif()
#see https://stackoverflow.com/questions/10851247/how-to-activate-c-11-in-cmake
if (CMAKE_VERSION VERSION_LESS "3.1")
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "--std=gnu++11 ${CMAKE_CXX_FLAGS}")
endif()
else()
set(CMAKE_CXX_STANDARD 11)
endif()
#set the source directory
file(GLOB SOURCES src/*.cpp)
add_subdirectory(py_shape)
#define sources and executable
add_executable(${PROJECT_NAME} ${SOURCES})
add_dependencies(${PROJECT_NAME} python_shape)
py_shape/CMakeLists.txt
#set file variables
file(GLOB SOURCE src/*.cpp)
file(GLOB HEADERS inc/*.hpp)
#build the library
add_library(python_shape MODULE ${SOURCE})
set_target_properties(python_shape PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/python")
#drop "lib" from the library name
set_target_properties(python_shape PROPERTIES PREFIX "")
if(WIN32)
#set extension to ".pyd"
set_target_properties(python_shape PROPERTIES SUFFIX ".pyd")
endif()
Now python_shape.pyd is created in python subdirectory.
What I have changed/removed:
Setting the ..._OUTPUT_DIRECTORY global variables in the child CMakeLists.txt is not necessary and/or won't work
Added LIBRARY_OUTPUT_DIRECTORY to overwrite the output directory for python_shape MODULE library target (see also e.g. Custom Directory for CMake Library Output)
Removed the "by config" settings, because I think it's a feature that multi-configuration make environments like Visual Studio put different configuration binaries into equally named sub-folders.
Added some if statements around -std=c++11. It has also only to be set once in the main CMakeLists.txt
You don't need to create the output directories "manually", they are created automatically by the make environments
For the reasoning why your first approach didn't work, see my comments above.
And I wouldn't recommend the use of file(GLOB ...) to collect source files (see e.g. Why is cmake file GLOB evil? or CMake/Ninja attempting to compile deleted `.cpp` file).
Alternatives
As with all languages/frameworks like CMake there is more then one way to do things.
E.g. you could also use POST_BUILD steps to copy your binaries to a common output directory:
see Cmake: use add_custom_command to copy binary to specific location failed when location doesn't exist
and - a little more up-to-date utilizing "generator expressions" - CMake: how to specify different steps for different build configurations for Visual Studio?

Deploy Qt5 QML application

To test QML deployment I've created a very simple QML application. Here is the code:
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QFile>
int main(int argc, char **argv) {
QApplication app(argc, argv);
QQmlApplicationEngine engine;
QString path = app.applicationDirPath() + "/qml/main.qml";
if(QFile::exists(path))
engine.load(path);
else {
return 1;
}
return app.exec();
}
main.qml
import QtQuick 2.2
import QtQuick.Controls 1.2
ApplicationWindow {
id: mainWindow
title: "Test window"
width: 800
height: 600
visible: true
}
To be sure no development library was installed in the system, I've set up a virtual machine with a pure Windows XP installation. Then, I've followed instructions as described here and copied all Qt5*.dll into the program directory, as well as platforms/qwindows.dll and icu*52.dll. Dependency Walker confirmed that no broken dependencies were left, i.e. everything should have been correctly set up.
However, for some reasons, when I run my app I see nothing. Neither a window, nor an error message. Running from console also gives me no error. Despite this, I can see my app running in the Task manager, like it is running in background. Running the app on the development machine goes without problem: the app correctly starts and I can see its windows.
What am I doing wrong? How can I deploy a QML app to be sure it will work on any other - non development - machine?
If you use MinGW, then try to copy all folders from folders qml and plugins to directory with your program. Also copy libraries: icudt52.dll, icuin52.dll, icuuc52.dll, libgcc_s_dw2-1.dll, libstdc++-6.dll, libwinpthread-1.dll, Qt5Core.dll, Qt5Gui.dll, Qt5Network.dll, Qt5Qml.dll, Qt5Quick.dll, Qt5Svg.dll, Qt5Widgets.dll from bin
Eventually the directory will look like this:
Enginio
imageformats
platforms
Qt
QtGraphicalEffects
QtPositioning
QtQml
QtQuick
QtQuick.2
QtSensors
QtWebKit
QtWinExtras
icudt52.dll
icuin52.dll
icuuc52.dll
libgcc_s_dw2-1.dll
libstdc++-6.dll
libwinpthread-1.dll
Qt5Core.dll
Qt5Gui.dll
Qt5Network.dll
Qt5Qml.dll
Qt5Quick.dll
Qt5Svg.dll
Qt5Widgets.dll
YOUR_PROGRAM.exe
This way works on WindowsXP/Win7 where Qt was not installed.
This what i've figured out so far,
You can't just open a qml file in main.cpp, you have to put those qmls into a resource
qml.qrc:
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
Then main.cpp must load it from the resource
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
then build and check it works, then deploy as follows:
locate the releasse directory where your EXE lives
locate the directory where your QML lives
create a directory somewhere, say, deploy
then
cd deploy
windeployqt --release --qmldir <qml-dir-location> <exe-location>
NOTE: add location of windeployqt to PATH
eg. C:\Qt\Qt5.5.1\5.5\msvc2013\bin
You should use the deployment tool that comes with Qt. See Qt for Windows - Deployment, there is a section "the Windows deployment tool".
As mentioned by BaCaRoZzo, a general solution that worked pretty well for me is to follow this guide.
To sum it up, copy in an empty directory:
The release version of MyApp.exe, along with .dll you created for this projet
All the .dll files from \mingw48_32\bin\
All the folders from \mingw48_32\plugins\
(If you used QML) All the folders from \mingw48_32\qml\
First, during the test of your application, rename your qt folder so it is not found by $PATH, and double-click on MyApp.exe. This should run the program.
NB: It leads to a very big application, so you will need to delete some extra files and folders.
The easiest way is for the dll: you run the program, and while being run, you delete all the dll in your new project. Only the ones that are not used by MyApp.exe will be deleted. Efficient!
For the Qt folders, proceed by trials and errors.
You need to deploy the application, for this purpose I use the utility
cqtdeployer
This utility itself collects all the necessary dependencies of your application and you do not have to spend your time on it, or you can automate this process.
You can install from github releases (Windows)
or
from snapstore (Linux)
sudo snap install cqtdeployer
You can use as follows:
Windows:
%cqtdeployer% -bin myApp -qmake path/to/Qt/5.x.x/build/bin/qmake.exe -qmlDir path/to/my/qml/files/dir
Linux:
cqtdeployer -bin myApp -qmake path/to/Qt/5.x.x/build/bin/qmake -qmlDir path/to/my/qml/files/dir
path/to/Qt/5.x.x/build/bin/qmake - This is the way qmake is used to build your program.
path/to/my/qml/files/dir - this is the path directly to your qml file (which you wrote)
And Run application with sh script (Linux) or exe (Windows)
If you'll use the version from snap then make sure that you have all the permissions.
If you need use windows version just install application from installer
use windeployqt --qmldir f:\myApp\sources f:\build-myApp\myApp.exe command.
the first argument after qmldir is your qml folder location and the second argument is where your exe is placed. You would get deployable exe on any env.
For me this answer worked for a QtQuick 2 application (QML app), but I'm adding more details:
Build inside Qt as Release
Find the folder in the file explorer with the executable, usually on a sibling folder to your project folder
Copy executable to an empty folder
Start Qt CMD tool (mine is ), go to the folder with the executable and run:
windeployqt --release --qmldir C:\Qt\5.15.1\msvc2015_64\qml executable.exe
(change 5.15.1\msvc2015_64 for yours)
My answer was:
(1) On Qt Framework set the Release mode for my Application.
(2) After: Project -> Build directory and set a empty directory in the "C:/" folder (I'm development Windows 10). Example: My Folder: "Release" with subfolder "My_app", so the Build directory is "C:/Release/My_app"
(3) Click on "Start debuggin of the startup project"
(4) Create another folder on "C:/" directory (in my case callet "Executable" with subfolder "My_app").
(5) Copy "My_app.exe" of the folder "Release/My_app/release" to "C:/Executable/My_app").
(6) Open Qt mingw.
(7) On the terminal of the Qt MinGw go to folder "Release/My_app/release" through command "cd C:/Executable/My_app"
(8) Run the command "windeploy.exe My_app.exe --qmldir 'full_path_where_are_the_qml_files'" (e.g.: "C:/My_prj/qmlsource", where qmlsource is folder with my qml_files).
This generate all files to run My_app.exe. Just the copy all folder (My_prj folder).

What is the proper way of deploying C++ QML plugins on mobile device?

I've been playing a lot with the Box2D QML plugin and the things are looking really good.
However, I wanted to deploy my sample app on Android (SGS2), but I can't seem to get it work. Whether I try to run it on AVD or on the device, it doesn't work. androiddeployqt finishes successfully, but then I get "Unable to start 'MyApp'" and no other info as to why it failed to start. I can successfully run qml apps on the AVD and the device, but this has something to do with the plugin and I can't find any reference in order to solve it.
I tried setting up DEPLOYMENTFOLDERS in different ways, but if i get them wrong, then the whole thing fails. Even when I don't get an error, in which case I assume I got it right, it still doesn't start.
I've been struggling with this for quiet some time, and can't find any bit of useful information in order to resolve it.
If you know of any project that uses a c++ plugin and can be successfully deployed on android device, that would be good as well.
I am using Qt 5.2.0 compiled for android and the qt5 branch of box2d
We were getting "module not found" errors while we were trying to get our QML module working inside an Android Qt application. With Qt 5.3, we managed to get our QML plugin recognized only by deploying the plugin to the QT_INSTALL_QML directory where official Qt QML modules reside. This directory is /opt/Qt/5.3/android_armv7/qml in our case.
Plugin Side
Our .pro file for the plugin looks like:
TEMPLATE = lib
TARGET = prova
QT += qml quick multimedia
CONFIG += qt plugin c++11 console
CONFIG -= android_install
TARGET = $$qtLibraryTarget($$TARGET)
uri = com.mycompany.qmlcomponents
# Input
SOURCES += \
src1.cpp \
src2.cpp
HEADERS += \
src1.h \
src2.h
##The below is generated automatically by Qt Creator when you create a new "Qt Quick 2 Extension Plugin" project for Android
#Copies the qmldir file to the build directory
!equals(_PRO_FILE_PWD_, $$OUT_PWD) {
copy_qmldir.target = $$OUT_PWD/qmldir
copy_qmldir.depends = $$_PRO_FILE_PWD_/qmldir
copy_qmldir.commands = $(COPY_FILE) \"$$replace(copy_qmldir.depends, /, $$QMAKE_DIR_SEP)\" \"$$replace(copy_qmldir.target, /, $$QMAKE_DIR_SEP)\"
QMAKE_EXTRA_TARGETS += copy_qmldir
PRE_TARGETDEPS += $$copy_qmldir.target
}
#Copies the qmldir file and the built plugin .so to the QT_INSTALL_QML directory
qmldir.files = qmldir
unix {
installPath = $$[QT_INSTALL_QML]/$$replace(uri, \\., /)
qmldir.path = $$installPath
target.path = $$installPath
INSTALLS += target qmldir
}
Our qmldir (in the plugin source tree root) file is:
module com.mycompany.qmlcomponents
plugin prova
Application Side
The .pro file looks like:
TEMPLATE = app
QT += qml quick widgets multimedia
CONFIG+= console
SOURCES += main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
contains(ANDROID_TARGET_ARCH,armeabi-v7a) {
ANDROID_EXTRA_LIBS = \
/opt/Qt/5.3/android_armv7/qml/com/mycompany/qmlcomponents/libprova.so
}
Important Note: Any library that your qml plugin uses must also be listed in ANDROID_EXTRA_LIBS in order to be bundled into the apk. This includes Qt components as well, listing them in QT+= is not enough if you don't use them in your application.
We don't actually know if the inclusion of the extra libprova.so is necessary. It's most probably not.
The main.cpp looks like:
#include <QApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[]){
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
The main.qml just includes the plugin like:
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtMultimedia 5.0
import com.mycompany.qmlcomponents 1.0
...
Building and Deployment
The way we build and deploy the plugin is to qmake (from the android-armv7 toolchain of Qt), then make install. It installs the qmldir file and the plugin .so to the QT_INSTALL_QML directory.
The way we build and deploy the project that uses the plugin is to qmake (again, from the android-armv7 toolchain of Qt), then make install INSTALL_ROOT=. (installs to build directory), then run androiddeployqt. The last command creates the Android project structure with the qmldirs in assets/ and libraries in libs/ and bundles the whole thing in an apk via ant. For the details of this procedure, refer to http://qt-project.org/wiki/Android.
In short, we were only able to get our QML plugin recognized inside an Android project by putting it inside the private Qt qml directory. I hope this helps in some way.

Resources