简体   繁体   English

SVG图标不会出现在Qt5中

[英]SVG icons dont show up in 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. 我在我的应用程序中使用ressource文件中的SVG图标,但是当我运行应用程序时,图标不会显示。 Using jpg icons in the same way works pretty fine. 以相同的方式使用jpg图标非常好。

Problem 问题

Since Qt5.1 the framework has been modularized . Qt5.1开始 ,框架已经模块化 Most likely you are missing the svg module. 很可能你错过了svg模块。 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). 确保SVG模块已安装在您的系统上并已链接(使用qmake (Howto) ,cmake (Howto)或plain make)。 If it was linked successfully QImageReader::supportedImageFormats() will list SVG. 如果它已成功链接, QImageReader :: supportedImageFormats()将列出SVG。

If you're using cmake, you need something like this to link Svg Qt libraries. 如果你正在使用cmake,你需要这样的东西来链接Svg Qt库。

find_package(Qt5Svg REQUIRED)

target_link_libraries( ${APP_NAME} Qt5::Svg )

A full example (sorry ManuelSchneid3r) could be the following one. 一个完整的例子(对不起ManuelSchneid3r)可能是以下一个。

## 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 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM