Qt5 中不显示 SVG 图标
SVG icons dont show up in Qt5
我在我的应用程序中使用来自资源文件的 SVG 图标,但是当我 运行 应用程序时,图标只是不显示。以同样的方式使用 jpeg 图标效果很好。
问题
自 Qt5.1 the framework has been modularized。
您很可能缺少 svg 模块。该应用程序仍会编译而不会抱怨。
解决方案
确保 SVG 模块已安装在您的系统上并已链接(使用 qmake (Howto), cmake (Howto) or plain make). If it was linked successfully QImageReader::supportedImageFormats() 将列出 SVG。
如果你正在使用 cmake,你需要这样的东西来 link 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 )
我在我的应用程序中使用来自资源文件的 SVG 图标,但是当我 运行 应用程序时,图标只是不显示。以同样的方式使用 jpeg 图标效果很好。
问题
自 Qt5.1 the framework has been modularized。 您很可能缺少 svg 模块。该应用程序仍会编译而不会抱怨。
解决方案
确保 SVG 模块已安装在您的系统上并已链接(使用 qmake (Howto), cmake (Howto) or plain make). If it was linked successfully QImageReader::supportedImageFormats() 将列出 SVG。
如果你正在使用 cmake,你需要这样的东西来 link 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 )