【问题标题】:SVG icons dont show up in Qt5SVG 图标不会出现在 Qt5 中
【发布时间】:2015-03-24 13:29:07
【问题描述】:

我在我的应用程序中使用资源文件中的 SVG 图标,但是当我运行该应用程序时,图标只是不显示。以同样的方式使用 jpg 图标效果很好。

【问题讨论】:

    标签: c++ qt svg qt5


    【解决方案1】:

    问题

    Qt5.1 开始,框架一直是modularized。 您很可能缺少 svg 模块。该应用程序仍将编译而不会出现任何问题。

    解决方案

    确保 SVG 模块已安装在您的系统上并已链接(使用 qmake (Howto)、cmake (Howto) 或普通 make)。如果链接成功QImageReader::supportedImageFormats() 将列出 SVG。

    【解决方案2】:

    如果你使用 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 )
    

    【讨论】:

    • 这个例子根本不够用。这就是我引用qt doc 的原因。定义、编译器标志、包含目录、MOC 等呢?
    • 好吧,对不起,我只举了一个小例子。
    猜你喜欢
    • 1970-01-01
    • 2021-11-21
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    相关资源
    最近更新 更多