Qt5 Linux如何设置窗口图标图像 [英] Qt5 linux how to set window icon image

查看:76
本文介绍了Qt5 Linux如何设置窗口图标图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Qt设计器设计的UI,并用c ++编写了代码.我正在使用我希望将其作为我指定的图像,但一直无法弄清楚如何使其工作.

I have a UI I have designed in Qt designer and have written the code in c++. I am using .I would like to have that be an image that I specify, but have been unable to figure out how to get this to work.

我的项目目录结构如下

package
|--CMakeLists.txt
|--src
    |--main.cpp 
    |--MainWindow.cpp
    |--resources
       |--images
           |--kitty.png
       |--icons.qrc
|--include
    |--MainWindow.hpp
|--ui
    |--MainWindow.ui

我的CMakeLists.txt文件看起来像

My CMakeLists.txt file looks like,

cmake_minimum_required(VERSION 2.8.7)
project(shared_memory_transport_sliderboard)

find_package(catkin REQUIRED)

find_package(Qt5Widgets REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}     ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
add_definitions(${Qt5Widgets_DEFINITIONS})

set(INCLUDES_DIR ${PROJECT_SOURCE_DIR}/include)
set(SOURCES_DIR ${PROJECT_SOURCE_DIR}/src)

catkin_package(
INCLUDE_DIRS ${INCLUDES_DIR}
DEPENDS Qt5Widgets
)

# c++11
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX0X)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
    message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no     C++0x support. Please use a different C++ compiler.")
endif()

include_directories(
${CMAKE_CURRENT_BINARY_DIR}
${INCLUDES_DIR}
${catkin_INCLUDE_DIRS}
${Qt5Widgets_INCLUDE_DIRS}
)

set(QT_FORMS
ui/MainWindow.ui
)
set(QT_MOC 
    ${INCLUDES_DIR}/${PROJECT_NAME}/MainWindow.h
)
set(QT_SOURCES
    src/MainWindow.cpp
)

qt5_wrap_cpp(QT_MOC_HPP ${QT_MOC})
qt5_wrap_ui(QT_FORMS_HPP ${QT_FORMS})
qt5_add_resources(ui_resources ${PROJECT_SOURCE_DIR}/resources/icons.qrc)

add_executable(smt_sliderboard src/main.cpp ${QT_SOURCES}   ${QT_MOC_HPP} ${QT_FORMS_HPP} ${ui_resources})

target_link_libraries(smt_sliderboard ${catkin_LIBRARIES} Qt5::Widgets)

install(TARGETS smt_sliderboard
  ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

install(DIRECTORY include/${PROJECT_NAME}/
    DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
    FILES_MATCHING PATTERN "*.h")

在编译我的代码时,编译器会生成一个 qrc_icons.cpp.o 对象,但是当我运行该可执行文件时,该图标不会显示.我的icons.qrc文件如下所示,

When compiling my code the compiler does generate a qrc_icons.cpp.o object, but when I run the executable the icon doesn't show up. The icons.qrc file I have looks like,

<!DOCTYPE RCC>
<RCC version="1.0">
    <qresource prefix="/Icons">
        <file alias="kitty">images/kitty.jpg</file>
    </qresource>
</RCC>

我也尝试过在MainWindow.h中添加以下内容,

I have also tried adding the following in MainWindow.h,

QApplication myApp(argc, argv);
QIcon appIcon;
appIcon.addFile(":/Icons/kitty");
myApp.setWindowIcon(appIcon);

,这样编译就可以了,但是仍然没有图标.感觉我没有运气就尝试了一切,谢谢您的帮助!

and this compiles just fine, but still no icon. Feel like I have tried everything with no luck, thanks for the help!

推荐答案

来自

请注意,列出的资源文件必须位于相同的位置目录作为.qrc文件或其子目录之一.

Note that the listed resource files must be located in the same directory as the .qrc file, or one of its subdirectories.

因此,您需要一个类似以下的目录结构:

So you need a directory structure something like:

|--images
    |--images
       |--kitty.png
    |--icons.qrc

更新:

您还应该参考Qt文档中的设置应用程序图标,确保您处理任何平台特定的问题.

You should also consult Setting the Application Icon in the Qt docs to make sure you deal with any platform-specific issues.

这篇关于Qt5 Linux如何设置窗口图标图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆