Search code examples
c++qtqgraphicsview

How to scale a QGraphicsEllipseItem with the Zoom Level of QGraphicsView


I have a QGraphicsView on a widget which im adding a number of items to.

some of these are polygons and some are ellipses (Both QGraphicsItems) when i zoom the Graphics view

void Test::on_verticalSlider_sliderMoved(int position)
{
    ui->graphicsView->scale(1.1,1.1);
}

the ellipses just get bigger and bigger but i want them to shrink so that they basically stay the same shape no matter how far zoomed in i am, so basically i want the polygons to be zoomed in on but not the points i have plotted onto these polygons...if that makes sense

dont know it makes it easier but all of the ellipse points plotted at any one time are within the same QGraphicsItemGroup so there could be a flag i can set on the entire group to do this?

anyway i would be greatful for any help with this

thanks

EDIT ------ CODE SAMPLE

item is the QGraphicsEllipseItem, and m_group_point is a QGraphicsItemGroup

 item->setParentItem(m_group_point.get());

then i add the group to a scene

m_scene2->addItem(m_group_point.get());

then add that scene to the view

ui->graphicsView->setScene(m_scene2.get());

they are .get because they are shared pointers


Solution

  • well, ignoreTransformation indeed a proper way to go.

    QGraphicsItem::ItemIgnoresTransformations: The item ignores inherited transformations (i.e., its position is still anchored to its parent, but the parent or view rotation, zoom or shear transformations are ignored). This flag is useful for keeping text label items horizontal and unscaled, so they will still be readable if the view is transformed. When set, the item's view geometry and scene geometry will be maintained separately. You must call deviceTransform() to map coordinates and detect collisions in the view. By default, this flag is disabled. This flag was introduced in Qt 4.3.

    About stay in the middle, you should move ellipses within scene to proper position, so they have proper scene coordinates, and then you instruct to ignore transformation of the view, so they will ignore any zooming/rotation/etc as mentioned in documentation.