我在我的应用程序中使用来自资源文件的SVG图标,但当我运行应用程序时,图标不会显示。以同样的方式使用jpg图标效果非常好。
问题
自Qt5.1以来,该框架已模块化。很可能您错过了svg模块。应用程序仍然可以编译而不会抱怨。
解决方案
确保SVG模块已安装在您的系统上并已链接(使用qmake(Howto)、cmake(Howto)或plain-Make)。如果链接成功,QImageReader::supportedImageFormats()将列出SVG。
如果您使用cmake,您需要这样的东西来链接Svg-Qt库。
find_package(Qt5Svg REQUIRED)
target_link_libraries( ${APP_NAME} Qt5::Svg )
一个完整的例子(对不起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 )