Hi, I know this is quite an old topic, but I needed the same functionality as paradiza. I noticed taht setSize() is mentioned in the docs here... http://doc.qt.nokia.com/4.6/qgraphic...m.html#details but apparently it's some mistake, since QGraphicsSvgItem does not have setSize() member.
So... I have subclassed the QGraphicsSvgItem like this:
Header:
#ifndef MYSVGITEM_H
#define MYSVGITEM_H
#include <QGraphicsSvgItem>
{
public:
//==================
// SETTERS & GETTERS
/*inl*/ void setSize(qreal width, qreal height);
//========================================================
// REIMPLEMENTATION OF VIRTUAL METHODS OF QGRAPHICSSVGITEM
private:
};
inline void MySvgItem
::setSize(qreal width, qreal height
) { setSize
(QSizeF(width, height
));
}
#endif // MYSVGITEM_H
#ifndef MYSVGITEM_H
#define MYSVGITEM_H
#include <QGraphicsSvgItem>
class MySvgItem : public QGraphicsSvgItem
{
public:
MySvgItem(QGraphicsItem* parent = 0);
MySvgItem(const QString& fileName, QGraphicsItem* parent = 0);
//==================
// SETTERS & GETTERS
void setSize(QSizeF size);
/*inl*/ void setSize(qreal width, qreal height);
QSizeF size();
//========================================================
// REIMPLEMENTATION OF VIRTUAL METHODS OF QGRAPHICSSVGITEM
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
QRectF boundingRect();
private:
QSizeF size_m;
};
inline void MySvgItem::setSize(qreal width, qreal height) { setSize(QSizeF(width, height)); }
#endif // MYSVGITEM_H
To copy to clipboard, switch view to plain text mode
Source:
#include "mysvgitem.h"
#include <QSvgRenderer>
#include <QStyleOptionGraphicsItem>
{
}
MySvgItem
::MySvgItem(const QString
& fileName,
QGraphicsItem* parent
){
}
{
//QGraphicsSvgItem::paint(painter, option, widget);
Q_UNUSED(widget);
Q_UNUSED(option);
if (!renderer()->isValid())
return;
if (elementId().isEmpty())
renderer()->render(painter, boundingRect());
else
renderer()->render(painter, elementId(), boundingRect());
}
void MySvgItem
::setSize(QSizeF size
) {
if (size_m != size) {
prepareGeometryChange();
size_m = size;
update(boundingRect());
}
}
{
qreal width = size_m.width();
qreal height = size_m.height();
if (size_m.width() < 0) {
width = (renderer()->boundsOnElement(elementId()).size().width());
}
if (size_m.height() < 0) {
height = (renderer()->boundsOnElement(elementId()).size().width());
}
}
QRectF MySvgItem
::boundingRect() {
}
#include "mysvgitem.h"
#include <QSvgRenderer>
#include <QStyleOptionGraphicsItem>
MySvgItem::MySvgItem(QGraphicsItem* parent)
: QGraphicsSvgItem(parent), size_m(-1.0, -1.0)
{
}
MySvgItem::MySvgItem(const QString& fileName, QGraphicsItem* parent)
: QGraphicsSvgItem(fileName, parent), size_m(-1.0, -1.0)
{
}
void MySvgItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
//QGraphicsSvgItem::paint(painter, option, widget);
Q_UNUSED(widget);
Q_UNUSED(option);
if (!renderer()->isValid())
return;
if (elementId().isEmpty())
renderer()->render(painter, boundingRect());
else
renderer()->render(painter, elementId(), boundingRect());
}
void MySvgItem::setSize(QSizeF size)
{
if (size_m != size) {
prepareGeometryChange();
size_m = size;
update(boundingRect());
}
}
QSizeF MySvgItem::size()
{
qreal width = size_m.width();
qreal height = size_m.height();
if (size_m.width() < 0) {
width = (renderer()->boundsOnElement(elementId()).size().width());
}
if (size_m.height() < 0) {
height = (renderer()->boundsOnElement(elementId()).size().width());
}
return QSizeF(width, height);
}
QRectF MySvgItem::boundingRect()
{
return QRectF(QPointF(0.0, 0.0), size());
}
To copy to clipboard, switch view to plain text mode
It works great... but there seems to be one limitation. Create your custom *.svg file and some element and remember it's size. Now when you try to use MySvgItem::setSize(...) and you set the size bigger that what the dimensions of the element in the *.svg file are, then the item is not rendered as a whole, but just a part of it is rendered (at maximum size of the element).
So it looks like this:
Setting the item's size up to the element's size works as it should.
Bookmarks