How to Use in Qt Project?
Important
Run
./build_icons.py --helpfor more latest information and full usages.
1. Clone this project into MY_QT_PROJ/resources/
git clone --recursive git@github.com:kuanyui/tango-remixer.git
2. Specify wanted icons via included_icons.txt
cd into MY_QT_PROJ/resources and create included_icons.txt: one icon per line, the theme and the icon name separated by a tab or by 4 or more spaces. For example:
Nome
Tango
Tango {16;22;48}
A line can also rename the icon, name its context, pick and convert sizes, and declare extra icon themes: see included_icons.txt Reference.
Note
Then you can test your
included_icons.txtlike this:./tango-remixer/build_icons.py --included-icon-file ./included_icons.txt --output-theme-name "CustomizedTango"The built SVG and PNG files can be found in
./tango-remixer/dist/. PNG is recommended because Qt's SVG renderer is not very well... Some icons may unable to be rendered correctly.
3. Edit CMakeLists.txt, watch the changes of included_icons.txt and build the included icons
Way 1: Use an extra "Stamp File" to track fresh / stale state
# ===============================================
# Build Icons
# ===============================================
find_package(Python3 REQUIRED Interpreter)
set(PROJ_GENERATED_DIR "${CMAKE_SOURCE_DIR}/generated" CACHE PATH
"Directory for generated, platform independent files (e.g. XDG icon theme, changelogs...etc)")
set(RESOURCES_DIR "${CMAKE_SOURCE_DIR}/resources")
set(ICON_REPO_DIR "${CMAKE_SOURCE_DIR}/resources/tango-remixer")
set(ICON_BUILD_SCRIPT "${ICON_REPO_DIR}/build_icons.py")
set(ICON_THEME_NAME "CustomizedTango")
set(ICON_PNG_XDG_THEME_DIR "${PROJ_GENERATED_DIR}/icon_theme__png/${ICON_THEME_NAME}")
set(ICON_SVG_XDG_THEME_DIR "${PROJ_GENERATED_DIR}/icon_theme__svg/${ICON_THEME_NAME}")
# When included_icons.txt changed, auto execute cmake again.
set(ICON_INCLUDE_FILE "${RESOURCES_DIR}/included_icons.txt")
set(ICON_GENERATED_STAMP_FILE "${RESOURCES_DIR}/icons_built.stamp")
message(STATUS "STAMP ============================+> ${ICON_GENERATED_STAMP_FILE}")
# If qrc does not exist, create empty placeholder
set(ICON_PNG_GENERATED_QRC "${ICON_PNG_XDG_THEME_DIR}/index.qrc")
if(NOT EXISTS ${ICON_PNG_GENERATED_QRC})
file(WRITE ${ICON_PNG_GENERATED_QRC} "<RCC>\n</RCC>\n")
endif()
add_custom_command(
OUTPUT ${ICON_GENERATED_STAMP_FILE}
COMMAND ${Python3_EXECUTABLE} ${ICON_BUILD_SCRIPT}
--included-icon-file "${RESOURCES_DIR}/included_icons.txt"
--extra-icon-theme "AppIcon:${RESOURCES_DIR}/appicon"
--output-theme-name "${ICON_THEME_NAME}"
--output-png-theme-to-dir "${ICON_PNG_XDG_THEME_DIR}"
--output-svg-theme-to-dir "${ICON_SVG_XDG_THEME_DIR}"
COMMAND ${CMAKE_COMMAND} -E touch ${ICON_GENERATED_STAMP_FILE}
DEPENDS ${ICON_INCLUDE_FILE}
COMMENT "Generating icon pack..."
USES_TERMINAL # immediately output
)
add_custom_target(icontheme DEPENDS ${ICON_GENERATED_STAMP_FILE})
# ==============================================
# Main
# ==============================================
qt_add_executable(myprojectname
MANUAL_FINALIZATION
${PROJECT_SOURCES}
${ICON_PNG_GENERATED_QRC}
add_dependencies(myprojectname icontheme)
Way 2: Brute Method (Always execute the build script after cmake)
# ===============================================
# Build Icons
# ===============================================
find_package(Python3 REQUIRED Interpreter)
set(RESOURCES_DIR "${CMAKE_SOURCE_DIR}/resources")
set(ICON_REPO_DIR "${CMAKE_SOURCE_DIR}/resources/tango-remixer")
set(ICON_BUILD_SCRIPT "${ICON_REPO_DIR}/build_icons.py")
execute_process(
COMMAND ${Python3_EXECUTABLE} ${ICON_BUILD_SCRIPT} --included-icon-file "${RESOURCES_DIR}/included_icons.txt" --output-theme-name "CustomizedTango"
)
set_property(
DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${RESOURCES_DIR}/included_icons.txt"
)
# ==============================================
# Main
# ==============================================
qt_add_executable(myprojectname
MANUAL_FINALIZATION
${PROJECT_SOURCES}
resources/tango-remixer/dist/04_FINAL_PNG_THEME/CustomizedTango/index.qrc
)
4. Use in Qt/C++
Now you can get QIcon via QIcon::fromTheme(). Setup mainwindow.cpp like this:
QIcon::setThemeSearchPaths({ ":/xdg_icon_themes" }); // this `:/xdg_icon_themes` is a `<qresource prefix>` defined in `qrc`
QIcon::setThemeName("CustomizedTango"); // `--output-theme-name`
setWindowIcon(QIcon::fromTheme("palettes")); // The included icon name.