How to reuse / copy SVG graphics items - revised



  • sorry about the repost - last post included an error

    I think I have it right now. At least it does what I want. This is what I ended up with. Maybe it will help someone else. There is more to do to make it fully functional but I can close this out unless you see any problems that might come back to haunt me. It would be great if there was a way to send images so you could see what it looks like ---

    Thanks again
    Bruce
    ////////////////////////////////////////////////////////////////////////////////////////////
    // LocalGraphicsItem .h
    ////////////////////////////////////////////////////////////////////////////////////////////
    #ifndef LOCAL_GRAPHICS_ITEM_H
    #define LOCAL_GRAPHICS_ITEM_H

    #include <QGraphicsItem>

    #include <qevent.h>
    #include <qpainter.h>

    class LocalGraphicsItem : public QGraphicsItem
    {
    public:
    LocalGraphicsItem(float xPos, float yPos, float scale, QGraphicsItem *letter);
    QRectF boundingRect() const Q_DECL_OVERRIDE;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) Q_DECL_OVERRIDE;

    protected:
    void paintEvent(QPaintEvent *event);

    private:
    QGraphicsItem *mSvgItem;
    float mXpos;
    float mYpos;
    float mScale;

    };

    #endif // LOCAL_GRAPHICS_ITEM_H

    ////////////////////////////////////////////////////////////////////////////////////////////
    #include "LocalGraphicsItem.h"
    ///////////////////////////////////////////////////////////////////////////////
    LocalGraphicsItem::LocalGraphicsItem(float xPos, float yPos, float scale, QGraphicsItem *letter)
    {
    mXpos = xPos;
    mYpos = yPos;
    mScale = scale;
    mSvgItem = letter;

    }

    ///////////////////////////////////////////////////////////////////////////////
    QRectF LocalGraphicsItem::boundingRect(void) const
    {
    QRectF rect;
    return(mSvgItem->boundingRect());

    }

    ///////////////////////////////////////////////////////////////////////////////
    // LocalGraphicsItem.cpp
    ///////////////////////////////////////////////////////////////////////////////
    void LocalGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    setX(mXpos);
    setY(mYpos);
    setScale(mScale);
    mSvgItem->paint(painter, option, widget);

    }
    ////////////////////////////////////////////////////////////////////////////////////////////
    // code that creates the text
    ////////////////////////////////////////////////////////////////////////////////////////////
    m_svgItem1 = new QGraphicsSvgItem("D:Font-Stroke/letter_A.svg");
    m_svgItem2 = new QGraphicsSvgItem("D:Font-Stroke/letter_B.svg");
    m_svgItem3 = new QGraphicsSvgItem("D:Font-Stroke/letter_C.svg");

    QRectF rect = m_svgItem1->boundingRect();

    float baseX = 10.0f;
    float upperCaseY = 10.0f;

    float upperCaseScale = 1.0f;
    float lowerCaseScale = 0.5f;

    float upperCaseWidth = rect.width();
    float lowerCaseWidth = rect.width() * lowerCaseScale;

    float lowerCaseY = upperCaseY + rect.height() * lowerCaseScale;

    mLocalGraphicsItem1 = new LocalGraphicsItem(baseX, upperCaseY, upperCaseScale, m_svgItem1);
    baseX += upperCaseWidth;

    mLocalGraphicsItem2 = new LocalGraphicsItem(baseX, lowerCaseY, lowerCaseScale, m_svgItem2);
    baseX += lowerCaseWidth;

    mLocalGraphicsItem3 = new LocalGraphicsItem(baseX, lowerCaseY, lowerCaseScale, m_svgItem1);
    baseX += lowerCaseWidth;


  • Qt Champions 2016

    @bclay
    Hi
    I love to see image !
    You can just upload to http://postimage.org/
    and post link :)

    Good work.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.