3

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,enter image description here .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!

jlack
  • 305
  • 2
  • 13

1 Answers1

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Nice catch! I updated my directory structure accordingly and still no dice though.. – jlack Mar 27 '16 at 01:08
  • Surely that should be: `appIcon.addFile(":/images/kitty.png");`? – ekhumoro Mar 27 '16 at 01:41
  • I think I have it correct right? Check out the stuff [here](http://doc.qt.io/qt-5/resources.html#resource-collection-files-op-op-qrc) about the ```prefix``` and ```alias``` – jlack Mar 27 '16 at 01:51
  • Well, yes, I know how it works - but I can't see what's in your qrc file. and your original question didn't appear to use an alias. Have you actually checked to see if you can read the resource using `QFile`? – ekhumoro Mar 27 '16 at 02:35
  • Oops sorry, I added the contents of the .qrc file to the question. Also I created a ```QFile``` like ```QFile test(":/Icons/kitty")``` and it compiled and ran just fine. – jlack Mar 27 '16 at 15:53
  • @jlack. Just to be clear: did you actually open and **read** the resource? Or as a simpler test, can you create a pixmap and display it in a `QLabel`? And another thing which is not completely clear is what you mean by "application icon". Are you talking about icons in the title-bar, or on the desktop? Because if it's the latter, it's a platform-specific thing (see my updated answer). – ekhumoro Mar 27 '16 at 16:24
  • I don't really know how to display a pixmap in a ```QLabel```. I'm kind of a Qt noob. And I don't mean a desktop icon. I really just mean the title bar icon. Like for example(on Ubuntu Unity) when you hit Alt+tab and you can scroll through the list of opened applications. – jlack Mar 27 '16 at 16:55
  • @jlack. `label->setPixmap(QPixmap(":/Icons/kitty"));`. – ekhumoro Mar 27 '16 at 17:14
  • @jlack. So it's a plaform-specific issue with ubuntu unity. See the link in my answer - you probably need to create a .desktop file. – ekhumoro Mar 27 '16 at 18:52
  • I got it working. Not actually 100% sure what made it work. I switched my window manager to Gnome-Shell, but I don't think right when I switched it the Icon showed up. I also changed ```QIcon appIcon; appIcon.addFile(":/Icons/kitty"); myApp.setWindowIcon(appIcon);``` to ```QIcon appIcon(":/Icons/kitty") myApp.setWindowIcon(appIcon);``` – jlack Mar 27 '16 at 20:47