Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-12108

QSvgRenderer render does not work properly when it's passed elementId and rect bigger than the size of the element in the *.svg file

    XMLWordPrintable

Details

    • Bug
    • Resolution: Incomplete
    • P3: Somewhat important
    • Some future release
    • 4.6.3
    • SVG Support
    • None
    • GNU/Linux openSuse 11.2

    Description

      Hi... suppose we have following class where we want to implement setSize() function for QGraphicsSvgItem

      Header
      #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
      
      Source
      #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());
      }
      

      Now, when you setSize() of some MySvgItem object (which is set to render only one element (setElementId(..))) to size bigger than the size which is set to the element in the source svg file, the result is cropped (as seen in the attached image). To prove that it's really problem of the renderer one can set the size of the actual element in the source svg file to big numbers and voila, it suddenly works (unless you set the size of the MySvgItem object too big).

      Note: This bug report might have connection with those two bug reports http://bugreports.qt.nokia.com/browse/QTBUG-5151 and http://bugreports.qt.nokia.com/browse/QTBUG-10903 (the former is closed because it's supposedly a feature and in the latter is provided workaround which does not work).

      Attachments

        1. main.cpp
          2 kB
          Qt Support
        2. step13.png
          6 kB
          Petr Pilar
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Unassigned Unassigned
            palmik Petr Pilar
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes