Arker
Arker

Reputation: 179

Qt QGraphicsSvgItem scaleing and resizeing

I created class inhereting after QGraphicsSvgItem and named it MyObject. I wish to assign an icon to it but at the same time I want to be able to rescale it so i can use one icon to present different objects (for example Door-Icon.svg is used to present small doors and big gates). Now there is a problem with it - there is no option to rescale image from *.svg itself. I tried working around with QPixmap, but it gave me pixeleted iamges.

  class MyObject : public QGraphicsSvgItem
  {
      void assign_Icon(QString Path);
      void rescale_Icon(QString Path);
      int Widith;
      int Height;
        // ...
  }

And function for:

  void MyObject::assign_Icon(QString Path)
  {
      QSvgRenderer *renderer = new QSvgRenderer(Path);
      this->setSharedRenderer(renderer);
      Widith =  this->renderer()->defaultSize().width();
      Height =  this->renderer()->defaultSize().height();
  }

It works fine just to present *.svg in its basic size, but I can't manage to find "how" I could resize that *.svg icon to object current width and height.

  void MyObject::rescale_Icon(QString Path)
  {
      QSvgRenderer *renderer = new QSvgRenderer(Path);
      this->setSharedRenderer(renderer);
      // But what to do here?
  }

Upvotes: 0

Views: 2133

Answers (2)

Arker
Arker

Reputation: 179

Resolved it with:

    QRectF MyObject::boundingRect() const
    {
        return QRectF(0,0,Widith,Height);
    }

    void MyObject::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget)
    {
        this->renderer()->render(painter, boundingRect());
    }

Upvotes: 4

Alex Borsuk
Alex Borsuk

Reputation: 21

void GraphicsColorSvgItem::paint(QPainter* painter,
                             const QStyleOptionGraphicsItem*option,
                             QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    _renderer.load(initFileSvg().toUtf8());

    painter->setRenderHint(QPainter::Antialiasing, true);
    painter->fillPath(shape(), Qt::NoBrush);

    //Start draw SVG file and set Rect
    _renderer.render(painter,QRect(0,
                               0,
                               getItemSize().width(),
                               getItemSize().height()));
    _fileSvg.close();
    painter->setRenderHint(QPainter::Antialiasing, false);
}
QString GraphicsColorSvgItem::initFileSvg()
{
    _fileSvg.setFileName(getFileName());
    _fileSvg.open(QIODevice::ReadOnly | QIODevice::Text);
    _str.setDevice(&_fileSvg);
    _content = _str.readAll();

    return _content;
}

Create Object this class GraphicsColorSvgItem itemSVG = new GraphicsColorSvgItem; and for scale need: itemSVG->setScale(scale_value);

Upvotes: 1

Related Questions