6

I am using SVG icons in my application from the ressource file, but when I run the app the icons are just not displayed. Using jpg icons in the same way works pretty fine.

ManuelSchneid3r
  • 15,850
  • 12
  • 65
  • 103

2 Answers2

9

Problem

Since Qt5.1 the framework has been modularized. Most likely you are missing the svg module. The application will still compile without complaining.

Solution

Make sure the SVG module is installed on your system and linked (with qmake (Howto), cmake (Howto) or plain make). If it was linked successfully QImageReader::supportedImageFormats() will list SVG.

ManuelSchneid3r
  • 15,850
  • 12
  • 65
  • 103
  • [Instruction to install qt5-svg on some Linux OS](https://stackoverflow.com/questions/21098805/unknown-modules-in-qt-svg). – user202729 Feb 15 '19 at 09:02
3

If you're using cmake, you need something like this to link Svg Qt libraries.

find_package(Qt5Svg REQUIRED)

target_link_libraries( ${APP_NAME} Qt5::Svg )

A full example (sorry ManuelSchneid3r) could be the following one.

## application name
set( APP_NAME "myapp" )

## project name
project( ${APP_NAME} )

## require a minimum version of CMake
CMAKE_MINIMUM_REQUIRED ( VERSION 2.6 FATAL_ERROR )

## add definitions, compiler switches, etc.
ADD_DEFINITIONS( -Wall -O2 )
SET( CMAKE_CXX_FLAGS -g )

## include (or not) the full compiler output
SET( CMAKE_VERBOSE_MAKEFILE OFF )

# find Qt
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Svg REQUIRED)

include_directories(${Qt5Widgets_INCLUDE_DIRS})
include_directories(${Qt5Core_INCLUDE_DIRS})
include_directories(${Qt5Gui_INCLUDE_DIRS})
include_directories(${Qt5Svg_INCLUDE_DIRS})

# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

# The AUTOUIC target property controls whether cmake(1) inspects the
# C++ files in the target to determine if they require uic to be run,
# and to create rules to execute uic at the appropriate time.
set(CMAKE_AUTOUIC ON)

## sources
file( GLOB MAIN_SRC *.cpp )
set( SOURCES ${MAIN_SRC} )

## executable
add_executable( ${APP_NAME} ${SOURCES} )

## link
target_link_libraries( ${APP_NAME} Qt5::Widgets Qt5::Svg )
Tarod
  • 6,732
  • 5
  • 44
  • 50
  • This example is not sufficient at all. Thats why I referenced the [qt doc](http://doc.qt.io/qt-5/cmake-manual.html). What about definitions, compiler flags, include dirs, MOC etc? – ManuelSchneid3r Mar 27 '15 at 12:35
  • Well, sorry, I only put a minimal example. – Tarod Mar 27 '15 at 12:40