Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG icons dont show up in Qt5

Tags:

c++

svg

qt

qt5

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.

like image 393
ManuelSchneid3r Avatar asked Mar 24 '15 13:03

ManuelSchneid3r


People also ask

How use SVG icon in I tag?

SVG images can be written directly into the HTML document using the <svg> </svg> tag. To do this, open the SVG image in VS code or your preferred IDE, copy the code, and paste it inside the <body> element in your HTML document. If you did everything correctly, your webpage should look exactly like the demo below.

Does Qt support SVG?

Qt supports the static features of SVG 1.2 Tiny. ECMA scripts and DOM manipulation are currently not supported. SVG drawings can be rendered onto any QPaintDevice subclass.

Can SVG files be used as icons?

Scalable Vector Graphics, or SVGs, are a generally a good choice for use as icons on your website, because they are vector graphics. Vector graphics can be scaled to any size without losing quality.


2 Answers

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.

like image 117
ManuelSchneid3r Avatar answered Sep 29 '22 22:09

ManuelSchneid3r


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 )
like image 39
Tarod Avatar answered Sep 29 '22 21:09

Tarod