vicrion
vicrion

Reputation: 1733

Why QIcon / QPixmap loaded image format should be png (and not svg) to be displayed in binary?

I have a Qt application where I load and display icons. This is a self-contained example of how I do it. In main.cpp I have:

Q_INIT_RESOURCE(Icons);
QApplication qapp(argc, argv);
QMainWindow window;
QAction* action = new QAction(QIcon(":/icon.png"), QObject::tr("&Icon"), &window);
QToolBar* tb = window.addToolBar(QObject::tr("Test"));
tb->addAction(action);

window.show();
return qapp.exec();

With Icons.qrc being the resource file.

My CMakeLists.txt contains:

# ...
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
find_package(Qt5 REQUIRED COMPONENTS Core Gui OpenGL)
set(SOURCES main.cpp)
qt5_add_resources(ICON_RSC Data/Icons.qrc)
add_executable(${PROJECT_NAME} ${SOURCES} ${ICON_RSC})
target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Gui Qt5::OpenGL)

Assume I create a binary of my application: I copy generated exe file and all the necessary Qt, platform and plugin dlls where they are supposed to be in application folder. My binary application runs with no problem if I use icon.png.

However, I want to use svg format. If I replace icon.png to icon.svg, the application compiles and runs from source, but the binary does not show the icon anymore.

My question is why does it happen? Is it something to do with missing library in my binary folder (I included imageformats folder and I can see qsvg.dll there, I also included iconengines folder with qsvgicon.dll inside); or Qt was designed not to use svg files for icons?

I tested the application on Qt5.4, Qt5.5 on Windows 7 and 10. Qt kits are MSVC2010 and MSVC2013 - 32bit.

Upvotes: 1

Views: 1251

Answers (1)

rubenvb
rubenvb

Reputation: 76539

You should link to Qt5::SVG and include the Qt XML library, cfr. here.

You can probably add both to your CMake file:

find_package(Qt5 REQUIRED COMPONENTS Core Gui OpenGL SVG XML)

and

target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Gui Qt5::OpenGL Qt5::SVG Qt5::XML)

Upvotes: 4

Related Questions