Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Qt plugins when linking to Qt statically?

Tags:

c++

icons

svg

qt

qt4

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.

like image 440
Thomi Avatar asked Feb 03 '23 09:02

Thomi


1 Answers

You need to use Q_IMPORT_PLUGIN.

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

like image 84
atomice Avatar answered Feb 06 '23 16:02

atomice