Results 1 to 5 of 5

Thread: [solved] Problems with QGraphicsSvgItem

  1. #1
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question [solved] Problems with QGraphicsSvgItem

    Hi,

    I want to use svg graphics in my QGraphicsView and so I found the QGraphicsSvgItem Class quickly.

    Qt Code:
    1. QGraphicsSvgItem item(":/standard.svg");;
    2. scene.addItem(&item);
    To copy to clipboard, switch view to plain text mode 

    It finds the file, but cannot load it:

    Cannot open file ':/standard.svg', because: Unknown error
    I created the svg file with inkscape so I thought it uses maybe a different svg syntax or something. So I used a svg of wikipedia, but had the same problem.

    I use Qt 4.3.0.

    Do you have an idea what I'm doing wrong?

    EDIT: I forgot to mention: Same problem if I use a QSvgRenderer.
    Last edited by grosem; 17th June 2007 at 18:29. Reason: updated contents

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QGraphicsSvgItem

    Are you sure you compiled the qrc?
    What happens if you give the absolute path of the file instead the path in the resource system?

    Put it somewhere accessible and give it a try.
    Regards

  3. #3
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Cool Re: Problems with QGraphicsSvgItem

    ok, there were indeed some problems with the resource file.

    Nevertheless I can't initialize the svg image directly:

    Qt Code:
    1. QGraphicsSvgItem item(":/standard.svg");
    To copy to clipboard, switch view to plain text mode 

    no error and the place for the image is reserved, but I can't see the image.

    It does work if I change the code to:

    Qt Code:
    1. QSvgRenderer *renderer = new QSvgRenderer(QLatin1String(":boards/standard.svg"));
    2.  
    3.  
    4. item->setSharedRenderer(renderer);
    5.  
    6. scene.addItem(item);
    To copy to clipboard, switch view to plain text mode 

    So do I really have to it this way? Can't I directly load the svg image?

    Thanks for your help

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QGraphicsSvgItem

    From Assistant:

    Detailed Description
    The QGraphicsSvgItem class is a QGraphicsItem that can be used to render the contents of SVG files.
    QGraphicsSvgItem provides a way of rendering SVG files onto QGraphicsView. QGraphicsSvgItem can be created by passing the SVG file to be rendered to its constructor or by explicit setting a shared QSvgRenderer on it.
    Note that setting QSvgRenderer on a QGraphicsSvgItem doesn't make the item take ownership of the renderer, therefore if using setSharedRenderer() method one has to make sure that the lifetime of the QSvgRenderer object will be at least as long as that of the QGraphicsSvgItem.
    QGraphicsSvgItem provides a way of rendering only parts of the SVG files via the setElementId. If setElementId() method is called, only the SVG element (and its children) with the passed id will be renderer. This provides a convenient way of selectively rendering large SVG files that contain a number of discrete elements. For example the following code renders only jokers from a SVG file containing a whole card deck:
    QSvgRenderer *renderer = new QSvgRenderer(QLatin1String("SvgCardDeck.svg"));
    QGraphicsSvgItem *black = new QGraphicsSvgItem();
    QGraphicsSvgItem *red = new QGraphicsSvgItem();
    black->setSharedRenderer(renderer);
    black->setElementId(QLatin1String("black_joker"));
    red->setSharedRenderer(renderer);
    red->setElementId(QLatin1String("red_joker"));
    Size of the item can be set via the setSize() method or via direct manipulation of the items transformation matrix.
    By default the SVG rendering is cached to speedup the display of items. Caching can be disabled by passing false to the setCachingEnabled() method.
    As you can see, you need a renderer.
    But you don't have to create the renderer every time( because it can be shared ).

    I suggest making a singleton class in your app that is created just after QApplication. This will have only one member: a renderer.

    Then, you can do something like:
    Qt Code:
    1. QSvgRenderer renderer = SingletonClass::getInstance()->getSharedRenderer();
    To copy to clipboard, switch view to plain text mode 

    This way the renderer can be shared between multiple classes.

    Another way is to make the renderer a member of your class.

    Regards

  5. The following user says thank you to marcel for this useful post:

    grosem (17th June 2007)

  6. #5
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QGraphicsSvgItem

    Ok, next time I read the documentation more carefully.

    And thanks for the hint

    edit: just to clarify - I don't have to use the renderer if I just want to load the svg. I don't know why it worked before, but now it does (sounds quite confusing, I know...). Anyway, it works
    Last edited by grosem; 17th June 2007 at 21:14.

Similar Threads

  1. Border cutting with QGraphicsSvgItem
    By akiross in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 09:12
  2. Replies: 2
    Last Post: 8th March 2007, 22:22
  3. Canvas problems
    By Tommytrojan in forum Qt Programming
    Replies: 22
    Last Post: 9th May 2006, 16:46
  4. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 15:39
  5. [Win32/VC++ 8.0] Strange problems with qrc_*.cpp files
    By mloskot in forum Installation and Deployment
    Replies: 6
    Last Post: 6th March 2006, 10:28

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.