PDA

View Full Version : resizing and moving a QGraphicsItem



xtraction
20th February 2012, 17:43
Hello,

I have a QGraphicsPathItem that I create after 'drawing' with the mouse on a scene. I have a background image in the scene and this path item gets created on top of the image which is almost transparent reflects a selection of an area of the underlying image. So basically I have my scene with two items.

Now when I press the zoom button I don't actually do and transformations to my scene or view, I simply compute the appropriate image somewhere else and then replace the image to the zoomed one. But now I need the drawn image also to take into account the zoom level and when I scroll to also scroll accordingly. I was trying to do this getting the boundingRect of the item and calculate a new bounding rect according to zoom and scroll position and then setting that calculated rectangle to be the new bounding rectangle of my item, so my item would resize itself and move to the right position whether I zoom or scroll. But this is not the right way since it gives very strange behavior. I do all the prepareForGeometryChange() before setting the boundign rectangle which I set in a function like this:



QRectF PathItem::boundingRect() const
{
return m_rect;
}

void PathItem::setBoundingRect(QRectF rect)
{
prepareGeometryChange();
m_rect = rect;
update(m_rect);
}




What would be the right way to do this? Basically how can I have an item in the scene move and rescale itself to a pre-calculated rectangle I provide it? Any hints and tips greatly appreciated.

Thanks.

xtraction
21st February 2012, 11:19
Well so far found out that when I set the bounding rectangle, it does change it to whatever I set it. I create my path Item drawing a path on the scene and then creating the item giving that path. So when I set it a new bounding rectangle I want the same exact path to be redrawn within the new bounding rectangle that I gave it. So far no matter what size of rectangle I set it to the item disappears, but I noticed that the outline of the bounding rectangle is there, just that the actual path item is not redrawn in the new bounding rectangle.
I keep the QPainterPath that I give it as a member variable so I am doing this:



void PathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QBrush brush(QColor(200, 200, 200));
painter->setBrush(brush);
painter->setOpacity(0.4);

m_path.setFillRule(Qt::WindingFill);
painter->drawPath(m_path);
}

QRectF PathItem::boundingRect() const
{
return m_rect;
}

QPainterPath PathItem::shape() const
{
return m_path;
}

void PathItem::setPath(const QPainterPath &_path)
{
QGraphicsPathItem::setPath(_path);
m_path = _path;
m_rect = path().boundingRect();
}

void PathItem::setBoundingRect(QRectF rect)
{
prepareGeometryChange();
m_rect = rect;
update(m_rect);
}


Where am I going wrong?