QGraphicsView and QGraphicsItem: don´t scale item when scaling the view rect

后端 未结 5 1627
既然无缘
既然无缘 2021-02-04 13:46

I am using Qt´s QGraphicsView - and QGraphicsItem-subclasses. is there a way to not scale the graphical representation of the item in the view when the

5条回答
  •  北荒
    北荒 (楼主)
    2021-02-04 14:19

    How about this:

    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char* argv[]) {
        QApplication app(argc, argv);
        QGraphicsScene scene;
        scene.addText("Hello, world!");
        QRect rect(50, 50, 100, 100);
        QGraphicsRectItem* recti = scene.addRect(rect);
        QGraphicsView view(&scene);
    
        // Set scale for the view
        view.scale(10.0, 5.0);
    
        // Set the inverse transformation for the item
        recti->setTransform(view.transform().inverted());
    
        view.show();
        return app.exec();
    }
    

    As you can see the text is scaled up but the rectangle is not. Note that this does not only prevent the scaling for the rectangle but and other transformation.

提交回复
热议问题