Loading Qt plugins when linking to Qt statically?


Question

I'm trying to use SVG graphics in QIcons. I have a static library that contains all my graphics resources, and a method in that static library that returns QIcons pre-loaded with the correct graphics. I'm trying to do this:

QIcon icon(":/icons/find.svg");

I have verified that that resources has been loaded by listing it with QDir:

qDebug() << QDir(":/icons/").entryList();

However, the SVG icon is not being shown. When I change the icon loading code to this:

QIcon icon(":/icons/find.png");

everything works correctly.

The problem seems to be that the SVG plugin never gets loaded. I can get a list of supported image types, which contains:

("bmp", "pbm", "pgm", "png", "ppm", "xbm", "xpm")

but no mention of SVG.

I've made sure I'm using the SVG module in both my static library .pro file, and my main application .pro file.

Edit:

It seems the problem is that I am linking to Qt statically, and thus the plugins are all static libs. The documentation for QPluginLoader states that:

Note that the QPluginLoader cannot be used if your application is statically linked against Qt. In this case, you will also have to link to plugins statically. You can use QLibrary if you need to load dynamic libraries in a statically linked application.

Yet statically linking to these plugins seems to do nothing ( the list of supported image formats does not grow).

Edit2:

Also, linking to Qt dynamically (after rebuilding my whole app) makes the code above work perfectly. However, I want to link to Qt statically, so the question looks now to be more about loading Qt plugins when linking to Qt statically and less about icons in general. I will update the post title to reflect this.

1
9
10/2/2009 9:36:52 AM

Accepted Answer

You need to use Q_IMPORT_PLUGIN.

See the documentation here: https://doc.qt.io/qt-5/qtplugin.html#Q_IMPORT_PLUGIN

11
1/14/2017 4:17:53 AM

According to documentation, the way to load a svg icon is:

QSvgRenderer svgRenderer( ":/icons/find.svg" );
Q_ASSERT( svgRenderer.isValid());

QImage img( svgRenderer.defaultSize() );
QPainter p( &img );
svgRenderer.render( &p );
p.end();

QIcon icon( QPixmap::fromImage( img ) );

For your link problem, QtSvg is part of Qt, so you do not need to add it as a plugin. The only thing required to compile with SVG support is :

QT += svg

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon