SVG图标不会显示在Qt5中
SVG icons dont show up in Qt5
我在我的应用程序中使用来自资源文件的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 )
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 控制台输出在 Qt5 中未正确显示
- 使用 C++ 在 Qt5 中显示曲线
- QWidgets 在 show() 之后不显示 -- c++/Qt5.
- Qt5:如何修改下载速度以显示 1.xx MB/s 而不是 1.xxxxx MB/s
- SVG图标不会显示在Qt5中
- qt5设计器,使用fileopen,在lineedit中显示文件路径,这样做是否有问题
- 在Qt5的QWidget中显示Qt3D内容
- 在生成的可执行文件中不显示Qt5图标
- Qt5部署的应用程序在QMovie中不显示GIF图像
- 在Qt5中显示FFmpeg帧的最佳/最简单的方法
- 突出显示与Qt5中的搜索字符串匹配的文本