Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to reuse / copy SVG graphics items - revised
Forum Update on Monday, May 27th 2025

How to reuse / copy SVG graphics items - revised

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 527 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • bclayB Offline
    bclayB Offline
    bclay
    wrote on last edited by
    #1

    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;

    mrjjM 1 Reply Last reply
    0
    • bclayB bclay

      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;

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Good work.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved