saqef
saqef

Reputation: 39

How to save QGraphicsItem sizes on screen while scaling view?

I've got map (QGraphicsPixmapItem) and cursor (inheritor of QGraphicsItem, just cross).

For cursor I have set QPen::setCosmetic(true)

When one zoom in/out scene by QGraphicsView::scale(), cursor keep right position and line width,but also change own size.

If activate QGraphicsItem::ItemIgnoresTransformations then line width and size are correct, but pos is wrong.

How to manage with it?

Upvotes: 0

Views: 315

Answers (2)

saqef
saqef

Reputation: 39

Solution: Pass view transform to your item. Reimplemented method setPos should be like:

void YourItem::setPos(const QPointF & pos)
{
    auto centerItem = m_boundRect.center(); //boundRect of your item
    centerItem.rx() /= m_viewTransf.m11();
    centerItem.ry() /= m_viewTransf.m22();
    QGraphicsItem::setPos(pos - centerItem);
}

Upvotes: 1

Kao
Kao

Reputation: 557

Keep the QGraphicsItem::ItemIgnoresTransformations flag and try to scale position. That should fix the problem.

const auto& pos = cursor->pos();
const QPointF newPos(pos.x() * scaleX, pos.y() * scaleY);
view->scale(scaleX, scaleY);
cursor->setPos(newPos);

Upvotes: 0

Related Questions