aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2021-09-01 15:53:43 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-09-03 15:10:44 +0000
commit1faf19bc486b89cd02e47f6b0b3f944400464ef9 (patch)
treee8f3c31a1aa154cbfb59889b98dfefaf2d85419a /examples
parent4bfe06bdcdf8c134d560bb4b07463b0944dcc518 (diff)
Install the "shared" example module into macOS bundles
If we build an application bundle we cannot rely on the relative path between the application's and the shared module's output directories. This is somewhat ugly, but as we don't have a comprehensive solution for building application bundles, yet, it's the best we can do right now. In order for the shared bundle to be loaded from the PlugIns directory, we need to add the PlugIns directory to the import path on macOS. Change-Id: I5b952420b4bb60af74886a140fa2c6a263d2f730 Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io> (cherry picked from commit 633a85cd39cdd294283439972cffebcff32ac0cb) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'examples')
-rw-r--r--examples/quick/shapes/CMakeLists.txt1
-rw-r--r--examples/quick/shared/CMakeLists.txt23
-rw-r--r--examples/quick/shared/shared.h9
-rw-r--r--examples/quick/touchinteraction/CMakeLists.txt1
-rw-r--r--examples/quick/views/CMakeLists.txt1
-rw-r--r--examples/quick/window/CMakeLists.txt1
-rw-r--r--examples/quick/window/main.cpp4
7 files changed, 40 insertions, 0 deletions
diff --git a/examples/quick/shapes/CMakeLists.txt b/examples/quick/shapes/CMakeLists.txt
index 33eb935d7d..ce83fd32cc 100644
--- a/examples/quick/shapes/CMakeLists.txt
+++ b/examples/quick/shapes/CMakeLists.txt
@@ -71,3 +71,4 @@ install(TARGETS shapesexample
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
)
+bundle_shared(shapesexample)
diff --git a/examples/quick/shared/CMakeLists.txt b/examples/quick/shared/CMakeLists.txt
index 9c24562eb3..7922e5b135 100644
--- a/examples/quick/shared/CMakeLists.txt
+++ b/examples/quick/shared/CMakeLists.txt
@@ -37,6 +37,29 @@ qt_add_qml_module(${PROJECT_NAME}_shared
"images/tab.png"
)
+# put the shared module into the macOS bundle directory.
+# Only call this function if your main project is has the MACOSX_BUNDLE option set.
+function(bundle_shared example)
+ if(QT6_IS_SHARED_LIBS_BUILD AND APPLE)
+ # The application's main.cpp adds an explicit QML import path to look for qml modules under
+ # a PlugIns subdirectory in a macOS bundle.
+ # Copy the qmldir and shared library qml plugin.
+
+ set(shared_dir "$<TARGET_FILE_DIR:${PROJECT_NAME}_shared>")
+ set(shared_qmldir_file "${shared_dir}/qmldir")
+ set(app_dir "$<TARGET_FILE_DIR:${example}>")
+ set(bundle_shared_dir "${app_dir}/../PlugIns/shared")
+
+ add_custom_command(TARGET ${example} POST_BUILD
+ COMMAND ${CMAKE_COMMAND} -E make_directory ${bundle_shared_dir}
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
+ $<TARGET_FILE:${PROJECT_NAME}_shared> ${bundle_shared_dir}
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
+ ${shared_qmldir_file} ${bundle_shared_dir}
+ )
+ endif()
+endfunction()
+
set(INSTALL_SHAREDDIR "${INSTALL_EXAMPLESDIR}/quick/${PROJECT_NAME}/shared")
install(TARGETS ${PROJECT_NAME}_shared
RUNTIME DESTINATION "${INSTALL_SHAREDDIR}"
diff --git a/examples/quick/shared/shared.h b/examples/quick/shared/shared.h
index 7f51b3d505..96bef11d98 100644
--- a/examples/quick/shared/shared.h
+++ b/examples/quick/shared/shared.h
@@ -52,6 +52,14 @@
#include <QQmlEngine>
#include <QQmlFileSelector>
#include <QQuickView> //Not using QQmlApplicationEngine because many examples don't have a Window{}
+
+#ifdef Q_OS_MACOS
+#define ADD_MACOS_BUNDLE_IMPORT_PATH \
+ view.engine()->addImportPath(app.applicationDirPath() + QStringLiteral("/../PlugIns"));
+#else
+#define ADD_MACOS_BUNDLE_IMPORT_PATH
+#endif
+
#define DECLARATIVE_EXAMPLE_MAIN(NAME) int main(int argc, char* argv[]) \
{\
QGuiApplication app(argc,argv);\
@@ -59,6 +67,7 @@
app.setOrganizationDomain("qt-project.org");\
app.setApplicationName(QFileInfo(app.applicationFilePath()).baseName());\
QQuickView view;\
+ ADD_MACOS_BUNDLE_IMPORT_PATH\
view.engine()->addImportPath(QStringLiteral(":/"));\
if (qEnvironmentVariableIntValue("QT_QUICK_CORE_PROFILE")) {\
QSurfaceFormat f = view.format();\
diff --git a/examples/quick/touchinteraction/CMakeLists.txt b/examples/quick/touchinteraction/CMakeLists.txt
index a5dd1416ba..5f7b0693ac 100644
--- a/examples/quick/touchinteraction/CMakeLists.txt
+++ b/examples/quick/touchinteraction/CMakeLists.txt
@@ -72,3 +72,4 @@ install(TARGETS touchinteractionexample
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
)
+bundle_shared(touchinteractionexample)
diff --git a/examples/quick/views/CMakeLists.txt b/examples/quick/views/CMakeLists.txt
index 18796e39ca..ca02f8d86e 100644
--- a/examples/quick/views/CMakeLists.txt
+++ b/examples/quick/views/CMakeLists.txt
@@ -95,3 +95,4 @@ install(TARGETS viewsexample
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
)
+bundle_shared(viewsexample)
diff --git a/examples/quick/window/CMakeLists.txt b/examples/quick/window/CMakeLists.txt
index a2340f700f..b8e81ff5c2 100644
--- a/examples/quick/window/CMakeLists.txt
+++ b/examples/quick/window/CMakeLists.txt
@@ -53,3 +53,4 @@ install(TARGETS windowexample
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
)
+bundle_shared(windowexample)
diff --git a/examples/quick/window/main.cpp b/examples/quick/window/main.cpp
index baf31bc846..7d2b0903fd 100644
--- a/examples/quick/window/main.cpp
+++ b/examples/quick/window/main.cpp
@@ -64,6 +64,10 @@ int main(int argc, char* argv[])
// Add the qrc root as QML import path so that the "shared" module can be found.
engine.addImportPath(QStringLiteral(":/"));
+#ifdef Q_OS_MACOS
+ engine.addImportPath(app.applicationDirPath() + QStringLiteral("/../PlugIns"));
+#endif
+
QQmlComponent component(&engine);
QQuickWindow::setDefaultAlphaBuffer(true);
component.loadUrl(QUrl("qrc:///window/window.qml"));