tags:

views:

837

answers:

2

Hi,

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.

A: 

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
Bluebird75
For normal applications, yes. However, icons are different. The QIcon docs (http://doc.trolltech.com/4.5/qicon.html) hint that with the svg icon engine you should be able to load SVG images directly into QIcons. Other people have had this work as well (http://lists.trolltech.com/qt-interest/2008-07/thread00105-0.html) just not in a static context.
Thomi
Sorry, I just realised that the above comment is ambiguous. "applications" in the first sentence is meant as in "uses", not as in "application programmes".
Thomi
+4  A: 

You need to use Q_IMPORT_PLUGIN.

See the documentation here: http://doc.trolltech.com/4.5/qtplugin.html#Q_IMPORT_PLUGIN

atomice
Thank you - that works perfectly.
Thomi