PDA

View Full Version : svg icons not showing up



ePharaoh
6th April 2006, 14:05
I have this code, which shows a blank widget!
:confused:



{
QToolButton *toolButton = new QToolButton(0);
QIcon *newTabIcon = new QIcon("newTab.svg");
toolButton->setIcon (*newTabIcon);
mainSplitter->addWidget(toolButton);
}


Is this the right way to use SVGs in qt 4.1.1?

jpn
6th April 2006, 14:17
It's not the right way to use SVG files, see QtSvg Module documentation (http://doc.trolltech.com/4.1/qtsvg.html).

ePharaoh
6th April 2006, 14:29
It's not the right way to use SVG files, see QtSvg Module documentation (http://doc.trolltech.com/4.1/qtsvg.html).

Thanks; I did, but it was rather complex for a QT newbie like me. Besides, it uses custom widgets.

Isn't it possible to use svg with default widgets which support pixmaps?

zlatko
6th April 2006, 14:37
I think that svg format has some another task for using

jpn
6th April 2006, 14:46
Remember to add "QT += svg" to your .pro file..



#include <QSvgRenderer>

void SvgToolButton::paintEvent(QPaintEvent* e)
{
// allow button to paint it's borders..
QToolButton::paintEvent(e);
// buffer is a member variable of type QPixmap
if (buffer.isNull() || buffer.size() != size() - margin)
{
// margin is a member variable of type QSize
buffer = QPixmap(size() - margin);
QString fileName("button.svg");
QSvgRenderer renderer(fileName);
QPainter painter(&buffer);
renderer.render(&painter);
}
// draw a pixmap on the button
QPainter painter(this);
painter.drawPixmap(margin.height()/2, margin.width()/2, buffer);
}


Edit: Btw, I did not test above code. Oh, and you might want to initialize the svg renderer somewhere else.. :p

jpn
6th April 2006, 15:26
A bit more complex but definitely more elegant way would be to implement a QIconEnginePlugin.


From QIcon docs:
With QIconEnginePlugin it is possible to register different icon engines for different file suffixes, so you could provide a SVG icon engine or any other scalable format.

ePharaoh
6th April 2006, 15:56
A bit more complex but definitely more elegant way would be to implement a QIconEnginePlugin.

Thanks for the leads, jpn. This is what I finally implemented, and I think it is elegant enough (posting here for the benefit of future noobs).



{
QToolButton *toolButton = new QToolButton(0);

QPixmap pixmap(24, 24);
QPainter painter(&pixmap);
painter.setBackgroundMode(Qt::OpaqueMode);
QSvgRenderer renderer (QString("newTab.svg"));
renderer.render (&painter);
QIcon newTabIcon(pixmap);

toolButton->setIcon (newTabIcon);
toolButton->setIconSize (QSize(24,24));
tabs.setCornerWidget (toolButton, Qt::BottomLeftCorner);
}