A Divendres 25 Abril 2008, Andreas Aardal Hanssen va escriure:
> Albert Astals Cid wrote:
> >> Hi, Albert. Can you please show the code that worked before and not now?
> >
> > I attach both the code that works with 4.4.RC1 and the one that worked on
> > 4.3
> > ToDraw inherits QGraphicsSvgItem
> > Basically the code "cuts" the SVG when it's outside some area.
> > I know it's a probably subobtimal way of doing it but it works well
> > enough for my use case.
> > As you see the only difference is the pos() call
>
> Hi, Albert. You can't use pos() to draw inside paint(); the paint()
> function renders in local coordinates. If you render using pos(), you'll
> always shift the rendering twice relative to the position of the item (but
> the painter is always translated with the item's position).

That's not true, it is translated now, it was not in Qt 4.3.

>
> Are you able to isolate this problem with simpler code? 

Of course i can, it's quite easy. Here it goes, along with the svg and the 
screenies of what happens in Qt 4.3 and 4.4-rc1

That behaviour change is not an issue to me anymore as i changed the way of 
doing what i did in the paint method and works on 4.3 and 4.4 now, but you 
should really be more careful when changing what parameters mean.

Albert


> The implementation 
> of paint() seems to be wrong. It looks like you're trying to implement your
> own "pixmap caching", such as QGraphicsSvgItem::setCachingEnabled in Qt 4.3
> or QGraphicsItem::setCacheMode in Qt 4.4. Could this be correct?
>
>         const QRectF &bounds = transform().mapRect(boundingRect());
>
> This maps your item's bounding rect using your item's transform. However,
> this transform does not compensate for the item's position. So depending on
> your item's position, bounds will be shifted.
>
>         const QRectF &backgroundBounds =
> m_background->transform().mapRect(renderer()->boundsOnElement("background")
>);
>
> I assume m_background is the parent item. This code maps the local SVG's
> background element bounding rect to the m_background's parent, again not
> adjusting for m_background's position. Note that the parent's transform and
> your item's local transform and both items' positions must be multiplied
> for this transformation to work.
>
>         double xMaxEdge = pos().x() - backgroundBounds.x() +
> bounds.width(); double yMaxEdge = pos().y() - backgroundBounds.y() +
> bounds.height();
>
> These variables seem to add the local position of the item to
> backgroundBounds top-left corner, I don't understand this code...
>
> If you look at mapToItem() or mapToScene(), these functions help you map
> coordinates correctly. Inside the source code of QGraphicsScene
> (qgraphicsscene.cpp), you'll see in the drawItemHelper function how we
> implement caching so that it works for all kinds of transforms.
>
> I don't know why your code has regressed in Qt 4.4, it's hard to say.
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsSvgItem>

class ToDraw : public QGraphicsSvgItem
{
	public:
		ToDraw() : QGraphicsSvgItem("norway.svg")
		{
		}
		
		void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget )
		{
			QGraphicsSvgItem::paint(painter, option, widget);
			
			painter->setPen(QPen(Qt::black));
			painter->setBrush(QColor(Qt::black));
			painter->drawRect(0, 0, 10, 10);
		}
};


int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	
	QGraphicsView *gv = new QGraphicsView();
	QGraphicsScene *scene = new QGraphicsScene();
	gv->setScene(scene);
	gv->show();
	gv->resize(800, 600);
	
	ToDraw *item = new ToDraw;
	item->setPos(QPoint(0,0));
	item->setZValue(0);
	scene->addItem(item);

	return a.exec();
}

<<attachment: norway.svg>>

<<qt43.png>>

<<qt44.png>>

Reply via email to