PDA

View Full Version : Non-scaling image added to scene



Thomas_Lerman
31st January 2011, 21:38
I am currently adding a PNG to the scene with addPixmap. It works just find, moving with the scene. However, I do not want the image to scale when the scene gets scaled. Any suggestions?

wysota
31st January 2011, 21:49
Set the ItemIgnoresTransformations flag.

Thomas_Lerman
31st January 2011, 22:01
Ah, how did I miss that? Thank you very much. The next thing that I need to do is to have the image centered on the position instead of painting from the top-left corner while still not scaling. I hope this is something just as simple that I have missed.

wysota
31st January 2011, 22:15
Most likely you mean QGraphicsView::alignment property (set it to Qt::AlignCenter).

Thomas_Lerman
31st January 2011, 22:26
I was not very clear with my last message, sorry. I am currently doing the following to add the PNG to the scene:
QPixmap *p = new QPixmap(":/some.png");
QGraphicsPixmapItem *d = NULL;
d = m_graphicsScene->addPixmap(*p);
d->setPos(SceneCoords.x(), SceneCoords.y());
d->setFlag(QGraphicsItem::ItemIgnoresTransformations, true);I would like this PNG centered on the scene coordinates while still not scaling. With the above code, the scene coordinates is the upper-left of the PNG.

wysota
31st January 2011, 22:29
Oh, I see. Try calling QGraphicsItem::setTransformOriginPoint() pointing to the center of your pixmap (boundingRect().center()). If it doesn't work then you'll have to subclass your item class.

Thomas_Lerman
31st January 2011, 23:13
For the fun of it, I added
d->setTransformOriginPoint((p->width() * 200), (p->height() * 200));between the above lines 4 & 5. I do not see any difference. :(

wysota
1st February 2011, 08:43
So subclass the pixmap item class and redefine the bounding rect to point to the center of the image. Also reimplement paint and translate the painter so that the original paint routine thinks it is still in top-left corner.

Thomas_Lerman
1st February 2011, 18:19
I did not find that changing the bounding rect was needed.

class MyGraphicsPixmapItem : public QGraphicsPixmapItem
{
public:
MyGraphicsPixmapItem(const QPixmap& pixmap, QGraphicsItem* parent = 0) :
QGraphicsPixmapItem(pixmap, parent) {};
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
painter->translate(-(option->rect.width() / 2), -(option->rect.height() / 2));
QGraphicsPixmapItem::paint(painter, option, widget);
}
};This is all I had to do. Did I miss anything? Thank you for your direction.

wysota
1st February 2011, 19:58
You have to change the bounding rect. Otherwise you'll get artifacts in an area that you paint and that is not inside your bounding rect.


QRectF MyGraphicsPixmapItem::boundingRect() const {
QRectF r = QGraphicsPixmapItem::boundingRect();
return r.translated(-r.width()*0.5, -r.height()*.0.5);
};

Thomas_Lerman
1st February 2011, 21:35
Excellent. I actually already had that in, but did not notice any difference. I now remember reading about the artifacts, so thank you for reminding me and you assistance!