Example: Add the app icon setup

The example's qmake setup includes the app icon config,
which is dropped in the CMake setup for some reason.

In addition, updated the app icon setup instructions
to use this example as a reference instead of the
snippet file.

Change-Id: I12bb8b007904164226dc78f2a2e162b08c9003e1
Fixes: QTBUG-83696
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
This commit is contained in:
Venugopal Shivashankar 2020-06-11 23:43:55 +02:00
parent 94a619a504
commit d48537ed2a
3 changed files with 36 additions and 40 deletions

View File

@ -63,7 +63,7 @@
Store the ICO file in your application's source code directory,
for example, with the name \c appico.ico.
\section2 Using CMake
\section2 Using CMake on Windows
To configure your application's icon, a resource file containing
information about the icon is required. A resource file is a text
@ -75,16 +75,16 @@
Once you have the \c .rc file, add information about the ICO file to it and
use it to configure your application icon.
The following snippet example demonstrates how to set up application
icon using CMake:
The following snippet demonstrates how the \l{Qt Quick Demo - Photo Surface}
example application uses CMake to set up an application icon:
\snippet snippets/code/CMakeLists.txt appicon_windows
\snippet demos/photosurface/CMakeLists.txt appicon_windows
Notice that the \c set command, defines the \c APP_ICON_RESOURCE_WINDOWS
Notice that the \c set command, defines the \c app_icon_windows
variable, which contains the path of the RC file. This variable is used
with the \c add_executable command to set the application's icon.
\section2 Using qmake
\section2 Using qmake on Windows
If you are still using qmake to generate your makefiles,
you need to add a single line to your \c .pro project file:
@ -127,26 +127,26 @@
icns files. Using this tool also compresses the resulting icns file,
so there is no need for you to perform additional compression.
\section2 Using CMake
\section2 Using CMake on \macos
To configure the application's icon, the \c Info.plist file generated
by CMake must contain the icon information. This can be achieved by
setting the \c .icns file name to the \c MACOSX_BUNDLE_ICON_FILE variable.
The following snippet demonstrates how to set up the application icon using
CMake:
The following snippet demonstrates how the \l{Qt Quick Demo - Photo Surface}
example application uses CMake to set up an application icon:
\snippet snippets/code/CMakeLists.txt appicon_macOS
\snippet demos/photosurface/CMakeLists.txt appicon_macOS
Notice that the first \c set command defines the \c MACOSX_BUNDLE_ICON_FILE
variable, which is required to add the icon file to the \c Info.plist file.
The second \c set command defines the \c APP_ICON_MACOSX variable with the
The second \c set command defines the \c app_icon_macos variable with the
absolute path to the icon file. This variable is then used to configure
MACOSX_PACKAGE_LOCATION, which defines the icon file's install location.
Finally, the \c add_executable uses the \c APP_ICON_MACOSX variable to set
Finally, the \c add_executable uses the \c app_icon_macOS variable to set
the application's icon.
\section2 Using qmake
\section2 Using qmake on \macos
If you are still using qmake to generate your makefiles, you only need
to add a single line to your \c .pro project file. For example,

View File

@ -1,24 +0,0 @@
cmake_minimum_required(VERSION 3.14)
project(qt5app LANGUAGES CXX)
if (WIN32)
#! [appicon_windows]
set(APP_ICON_RESOURCE_WINDOWS "${CMAKE_CURRENT_SOURCE_DIR}/qt5app.rc")
add_executable(qt5app main.cpp ${APP_ICON_RESOURCE_WINDOWS})
#! [appicon_windows]
elseif (APPLE)
#! [appicon_macOS]
# NOTE: Don't include the path in MACOSX_BUNDLE_ICON_FILE -- this is
# the property added to Info.plist
set(MACOSX_BUNDLE_ICON_FILE qt5app.icns)
# And this part tells CMake where to find and install the file itself
set(APP_ICON_MACOSX ${CMAKE_CURRENT_SOURCE_DIR}/qt5app.icns)
set_source_files_properties(${APP_ICON_MACOSX} PROPERTIES
MACOSX_PACKAGE_LOCATION "Resources")
add_executable(qt5app MACOSX_BUNDLE main.cpp ${APP_ICON_MACOSX})
#! [appicon_macOS]
else()
add_executable(qt5app main.cpp)
endif()

View File

@ -16,9 +16,29 @@ find_package(Qt6 COMPONENTS Gui)
find_package(Qt6 COMPONENTS Qml)
find_package(Qt6 COMPONENTS Quick)
add_qt_gui_executable(photosurface
main.cpp
)
if (WIN32)
#! [appicon_windows]
set(app_icon_resource_windows "${CMAKE_CURRENT_SOURCE_DIR}/resources/photosurface.rc")
add_executable(photosurface main.cpp ${app_icon_resource_windows})
#! [appicon_windows]
elseif (APPLE)
#! [appicon_macOS]
# The MACOSX_BUNDLE_ICON_FILE variable is added to the Info.plist
# generated by CMake. This variable contains the .icns file name,
# without the path.
set(MACOSX_BUNDLE_ICON_FILE photosurface.icns)
# And the following tells CMake where to find and install the file itself.
set(app_icon_macos "${CMAKE_CURRENT_SOURCE_DIR}/resources/photosurface.icns")
set_source_files_properties(${app_icon_macos} PROPERTIES
MACOSX_PACKAGE_LOCATION "Resources")
add_executable(photosurface MACOSX_BUNDLE main.cpp ${app_icon_macos})
#! [appicon_macOS]
else()
add_executable(photosurface main.cpp)
endif()
target_link_libraries(photosurface PUBLIC
Qt::Core
Qt::Gui