July 14, 2010

Palmik Palmik
Lab Rat
4 posts

Looking for help with optimalization of QGraphicsItem subclass

 

Hi guys. I have this QGraphicsSvgItem subclass (header [gitorious.org], source [gitorious.org]). It was working quite well up until I tried to add some QGraphicsEffects (QGraphicsDropShadowEffect to be precise) to objects of that class… what I noticed (1 minute after the application even loaded) is that it was not usable at all… resizing the window was out of question, and so was anything else, so I dumped the idea, because I thought the reason for that was the QGraphicsSvgItem itself. But I was not sure so I created a testcase where I tried to find the culprit… what I found out was that the MySvgItem::size() is the method causing the problems (probably because boundingRect() is calculated out of size()), because when I did not set the size, it worked quite fine in my testcase with 625 items, but when I set the size to any value, the lagfest was back. The problem is that I use the method quite extensively and as I’m writing this I still have no solution to make the size() work faster.

Here is the code I suspect from causing the overhead (the whole source code is linked above)

  1. void MySvgItem::setSize(const QSizeF& size)
  2. {
  3.     if (size_m != size) {
  4.         prepareGeometryChange();
  5.         size_m = size;
  6.         update(boundingRect());
  7.     }
  8. }
  9.  
  10. QSizeF MySvgItem::size() const
  11. {
  12.     qreal width = size_m.width();
  13.     qreal height = size_m.height();
  14.  
  15.     if (size_m.width() < 0) {
  16.         width = (renderer()->boundsOnElement(elementId()).size().width());
  17.     }
  18.  
  19.     if (size_m.height() < 0) {
  20.         height = (renderer()->boundsOnElement(elementId()).size().width());
  21.     }
  22.  
  23.     return QSizeF(width, height);
  24. }
  25.  
  26. QRectF MySvgItem::boundingRect()
  27. {
  28.     return QRectF(QPointF(0.0, 0.0), size());
  29. }

Here [palmik.net] is the compile-ready testcase (with svg and everything) I was using to test the stuff (the setSize() lines are now commented out). I would be glad for any help or other input. Thanks :)

1 reply

July 16, 2010

Palmik Palmik
Lab Rat
4 posts

So I rewrote some parts of the project so that it no longer relies on the setSize(..) (which I found to be useful not only because of the added performance but also because it’s gives more free space to the designer of the app (more things can be set directly in the svg file)).

Yet, there is one remaining problem and that is that when I use QSvgRenderer::render(QPainter* painter, const QString& elementId, const QRectF& bounds = QRectF()) and the bounds rectangle is bigger than QSvgRenderer::boundsOnElement(const QString& elementId) then it’s not rendered correctly (only part is rendered).

 
  ‹‹ VS2010 problem      SOLVED: Need help launching external apps from qtdesktopplugin ››

You must log in to post a reply. Not a member yet? Register here!