问题
I have a UI I have designed in Qt designer and have written the code in c++. I am using catkin, which is just cmake, to build my project. Currently when the program is launched, the application icon looks like, .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.
My project directory structure looks like the following
package
|--CMakeLists.txt
|--src
|--main.cpp
|--MainWindow.cpp
|--resources
|--images
|--kitty.png
|--icons.qrc
|--include
|--MainWindow.hpp
|--ui
|--MainWindow.ui
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")
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>
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!
回答1:
From the Qt Resource System docs:
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
UPDATE:
You should also consult Setting the Application Icon in the Qt docs to make sure you deal with any platform-specific issues.
来源:https://stackoverflow.com/questions/36241303/qt5-linux-how-to-set-window-icon-image