A Dimecres 09 Abril 2008, Andreas Aardal Hanssen va escriure:
> Albert Astals Cid wrote:
> > When overloading QGraphicsSvgItem::paint previously i had to do things
> > like painter->drawPixmap(pos(), ...) to get to the correct position of my
> > item, now it is
> > painter->drawPixmap(QPoint(0,0), ...)
> > In my opnion that's a bad thing.
> > Can we have that reverted? Or will i have to #ifdef for one Qt version
> > and another?
>
> 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
Albert
void ToDraw::paint(QPainter * painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
const QRectF &bounds = transform().mapRect(boundingRect());
const QRectF &backgroundBounds =
m_background->transform().mapRect(renderer()->boundsOnElement("background"));
double xMaxEdge = pos().x() - backgroundBounds.x() + bounds.width();
double yMaxEdge = pos().y() - backgroundBounds.y() + bounds.height();
double widthCut = 0;
double heightCut = 0;
double xStart = 0;
double yStart = 0;
if (xMaxEdge > backgroundBounds.width()) widthCut = xMaxEdge -
backgroundBounds.width();
if (yMaxEdge > backgroundBounds.height()) heightCut = yMaxEdge -
backgroundBounds.height();
if (pos().x() < backgroundBounds.left()) xStart =
backgroundBounds.left() - pos().x();
if (pos().y() < backgroundBounds.top()) yStart = backgroundBounds.top()
- pos().y();
if (widthCut > 0 || heightCut > 0 || xStart > 0 || yStart > 0)
{
QImage img(qRound(bounds.width()), qRound(bounds.height()),
QImage::Format_ARGB32_Premultiplied);
QPainter p2(&img);
p2.setCompositionMode(QPainter::CompositionMode_Clear);
p2.setBrush(Qt::SolidPattern);
p2.drawRect(0, 0, qRound(bounds.width()),
qRound(bounds.height()));
p2.setCompositionMode(QPainter::CompositionMode_SourceOver);
renderer()->render(&p2, elementId());
p2.end();
painter->setWorldMatrix(QMatrix());
painter->drawImage(pos() + QPointF(xStart, yStart), img,
QRectF(xStart, yStart, bounds.width() - widthCut, bounds.height() - heightCut));
}
else QGraphicsSvgItem::paint(painter, option, widget);
}void ToDraw::paint(QPainter * painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
const QRectF &bounds = transform().mapRect(boundingRect());
const QRectF &backgroundBounds =
m_background->transform().mapRect(renderer()->boundsOnElement("background"));
double xMaxEdge = pos().x() - backgroundBounds.x() + bounds.width();
double yMaxEdge = pos().y() - backgroundBounds.y() + bounds.height();
double widthCut = 0;
double heightCut = 0;
double xStart = 0;
double yStart = 0;
if (xMaxEdge > backgroundBounds.width()) widthCut = xMaxEdge -
backgroundBounds.width();
if (yMaxEdge > backgroundBounds.height()) heightCut = yMaxEdge -
backgroundBounds.height();
if (pos().x() < backgroundBounds.left()) xStart =
backgroundBounds.left() - pos().x();
if (pos().y() < backgroundBounds.top()) yStart = backgroundBounds.top()
- pos().y();
if (widthCut > 0 || heightCut > 0 || xStart > 0 || yStart > 0)
{
QImage img(qRound(bounds.width()), qRound(bounds.height()),
QImage::Format_ARGB32_Premultiplied);
QPainter p2(&img);
p2.setCompositionMode(QPainter::CompositionMode_Clear);
p2.setBrush(Qt::SolidPattern);
p2.drawRect(0, 0, qRound(bounds.width()),
qRound(bounds.height()));
p2.setCompositionMode(QPainter::CompositionMode_SourceOver);
renderer()->render(&p2, elementId());
p2.end();
painter->setWorldMatrix(QMatrix());
painter->drawImage(QPointF(xStart, yStart), img, QRectF(xStart,
yStart, bounds.width() - widthCut, bounds.height() - heightCut));
}
else QGraphicsSvgItem::paint(painter, option, widget);
}