View Full Version : scaling a QPixmap, devides Pixmap in different parts
airglide
25th November 2012, 19:50
Hello everyone,
I've written a application which resizes a QPixmap (which is added in a QGraphicsPixmapItem which is added to a QGraphicsScene). When I grab a corner, the QPixmap resizes (with a QTransform object). My Problem is, that the scaling isn't doing what I want. It's deviding the picture in 4 parts (see image).
Has someone an idea why this happens? Or is this a performance issue?
thank you
airglide
8455
ChrisW67
26th November 2012, 01:47
Has someone an idea why this happens? Or is this a performance issue?
Sure, you are doing something odd in your code. I only we could see the code...
airglide
26th November 2012, 06:42
I've set some Dots to grab and set the derived QPixmap class as a parentitem, then I've installed a sceneEventFilter
bool PixmapItem::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
{
GrabDot *dot = dynamic_cast<GrabDot *> (watched);
if(dot == NULL)
return false;
int expandX = 0;
int expandY = 0;
switch(event->type())
{
case QEvent::GraphicsSceneMousePress:
{
}break;
case QEvent::GraphicsSceneMouseRelease:
{
}break;
case QEvent::GraphicsSceneMouseMove:
{
QGraphicsSceneMouseEvent *e = dynamic_cast<QGraphicsSceneMouseEvent *>(event);
switch(dot->getDotNumber())
{
case 0:
{
expandX = -1;
expandY = -1;
//this->setPos(this->cursor().pos());
}break;
case 1:
{
expandX = 0;
expandY = -1;
}break;
case 2:
{
expandX = 1;
expandY = -1;
}break;
case 3:
{
expandX = 1;
expandY = 0;
}break;
case 4:
{
expandX = 1;
expandY = 1;
}break;
case 5:
{
expandX = 0;
expandY = 1;
}break;
case 6:
{
expandX = -1;
expandY = 1;
}break;
case 7:
{
expandX = -1;
expandY = 0;
}break;
}
int width = 0;
int height = 0;
//if side is dragged, save the new Bounding rect
bool sideDragged = false;
switch(dot->getDotNumber())
{
case 0:
case 2:
case 4:
case 6:
{
//if a corner is grabbed, the proportion has to stay the same
//only the change of the x value is interesting
width = boundingRect().width() + (expandX * e->pos().x());
height = width * propRect.height()/propRect.width();
sideDragged = false;
}break;
default:
width = boundingRect().width() +(expandX * e->pos().x());
height = boundingRect().height() +(expandY * e->pos().y());
sideDragged = true;
break;
}
QTransform trans = QTransform();
trans.scale(width/boundingRect().width(), height/boundingRect().height());
prepareGeometryChange();
setPixmap(pixmap().transformed(trans));
if(sideDragged)
{
saveBoundingRect();
}
switch(dot->getDotNumber())
{
case 0:
{
setPos(pos().x() + e->pos().x(), pos().y() + e->pos().y());
}break;
case 1:
{
setPos(pos().x(), pos().y() + e->pos().y());
}break;
case 2:
{
setPos(pos().x(), pos().y() + e->pos().y());
}break;
case 6:
{
setPos(pos().x() + e->pos().x(), pos().y());
}break;
case 7:
{
setPos(this->pos().x() + e->pos().x(), this->pos().y());
}break;
}
setGrabDotPos();
}break;
default:
{
return false;
}break;
}
thank you for your help ;)
prof.ebral
26th November 2012, 08:33
Why not save the original pixmap as an object ( in PyQt: self.originalPixmap = self.pixmap() ) and then when you scale the image set the scale from the original pixmap ( in PyQt: self.setPixmap(self.originalPixmap.scaled(width/boundingRect().width(), height/boundingRect().height()) )?
airglide
26th November 2012, 09:24
I wanted to implement that later, but now it works. I've added to my Pixmap class a QPixmap object called originalPixmap. I scale this image and display it.
great
thank you
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.