Results 1 to 5 of 5

Thread: How to adjust the initial size of a QGraphicsSvgItem without using scale() ?

  1. #1
    Join Date
    Feb 2007
    Posts
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Question How to adjust the initial size of a QGraphicsSvgItem without using scale() ?

    Hi all,

    is there a way one can adjust the initial size of a QGraphicsSvgItem without using QGraphicsItem::scale(sx, sy)?

    The problem is that I subsequently assign some children to this item which then inherit the transformation matrix of their parent, hence are also scaled. I need to set the initial size of my QGraphicsSvgItem before I assign its children and without changing their transformation matrices also.

    FYI, I'm migrating from QGraphicsPixmapItems to QGraphicsSvgItems. Using QGraphicsPixmapItem I could just resize its pixmap before assigning it to the item. Thus the item's size reflected the size of the pixmap - a similar resizing doesn't seem to be possible with QSvgRenderer...


    Thanks,
    para

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to adjust the initial size of a QGraphicsSvgItem without using scale() ?

    Seems that you are right. What you could do is to subclass the item and reimplement boundingRect(), shape() and paint() and introduce additional level of scaling there. This way it won't be propagated to children.

  3. #3
    Join Date
    Feb 2007
    Posts
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to adjust the initial size of a QGraphicsSvgItem without using scale() ?

    Ok, that's what I presumed. No problem, I already subclassed QGraphicsSvgItem anyway...

    Thanks mate!

  4. #4
    Join Date
    Jan 2009
    Location
    Czech Republic
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to adjust the initial size of a QGraphicsSvgItem without using scale() ?

    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:
    Qt Code:
    1. #ifndef MYSVGITEM_H
    2. #define MYSVGITEM_H
    3.  
    4. #include <QGraphicsSvgItem>
    5.  
    6. class MySvgItem : public QGraphicsSvgItem
    7. {
    8. public:
    9. MySvgItem(QGraphicsItem* parent = 0);
    10. MySvgItem(const QString& fileName, QGraphicsItem* parent = 0);
    11.  
    12. //==================
    13. // SETTERS & GETTERS
    14. void setSize(QSizeF size);
    15. /*inl*/ void setSize(qreal width, qreal height);
    16. QSizeF size();
    17.  
    18. //========================================================
    19. // REIMPLEMENTATION OF VIRTUAL METHODS OF QGRAPHICSSVGITEM
    20. void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
    21. QRectF boundingRect();
    22.  
    23. private:
    24. QSizeF size_m;
    25. };
    26.  
    27. inline void MySvgItem::setSize(qreal width, qreal height) { setSize(QSizeF(width, height)); }
    28.  
    29. #endif // MYSVGITEM_H
    To copy to clipboard, switch view to plain text mode 

    Source:
    Qt Code:
    1. #include "mysvgitem.h"
    2. #include <QSvgRenderer>
    3. #include <QStyleOptionGraphicsItem>
    4.  
    5. MySvgItem::MySvgItem(QGraphicsItem* parent)
    6. : QGraphicsSvgItem(parent), size_m(-1.0, -1.0)
    7. {
    8. }
    9.  
    10. MySvgItem::MySvgItem(const QString& fileName, QGraphicsItem* parent)
    11. : QGraphicsSvgItem(fileName, parent), size_m(-1.0, -1.0)
    12. {
    13. }
    14.  
    15. void MySvgItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
    16. {
    17. //QGraphicsSvgItem::paint(painter, option, widget);
    18.  
    19. Q_UNUSED(widget);
    20. Q_UNUSED(option);
    21.  
    22. if (!renderer()->isValid())
    23. return;
    24.  
    25. if (elementId().isEmpty())
    26. renderer()->render(painter, boundingRect());
    27. else
    28. renderer()->render(painter, elementId(), boundingRect());
    29. }
    30.  
    31. void MySvgItem::setSize(QSizeF size)
    32. {
    33. if (size_m != size) {
    34. prepareGeometryChange();
    35. size_m = size;
    36. update(boundingRect());
    37. }
    38. }
    39.  
    40. QSizeF MySvgItem::size()
    41. {
    42. qreal width = size_m.width();
    43. qreal height = size_m.height();
    44.  
    45. if (size_m.width() < 0) {
    46. width = (renderer()->boundsOnElement(elementId()).size().width());
    47. }
    48.  
    49. if (size_m.height() < 0) {
    50. height = (renderer()->boundsOnElement(elementId()).size().width());
    51. }
    52.  
    53. return QSizeF(width, height);
    54. }
    55.  
    56. QRectF MySvgItem::boundingRect()
    57. {
    58. return QRectF(QPointF(0.0, 0.0), size());
    59. }
    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.
    Last edited by Palmik; 9th July 2010 at 16:55.

  5. #5
    Join Date
    Jan 2009
    Location
    Czech Republic
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to adjust the initial size of a QGraphicsSvgItem without using scale() ?

    Hmm... it's probably some kind of bug in QSvgRenderer, for now I have "solved" the problem by having the elements extremely large in the *.svg file so that I can setSize to bigger numbers :/
    Edit: Might it be this bug http://bugreports.qt.nokia.com/browse/QTBUG-5151 ? They seem to have rejected it because it's supposedly feature.
    Last edited by Palmik; 11th July 2010 at 09:56.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.