Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Struggling with QGraphicsView



  • Hello there,

    I'm having hard time using QGraphicsView to display images.

    First, when I display an image for the first time, the image look very small despite the fact that I ask my view to fit the whole image.
    In fact, I tried to display the QGraphicsView size by calling qDebug and it appeared that my QGraphicsView size was 28x28 after startup and first display, and after a second display it has been updated to the good size (wich was 800x800).

    Also, I created two QPushButton to zoom in and out and I connected them with a slot that set the view scale. But when I click on the button, the view actually change but with one or more clicks of delay (not always the same).

    Here is my code :

    // CONSTRUCTOR
    scene = new QGraphicsScene(this);
    view = new GraphicsView(scene,this);
    zoom_factor = 1;
    
    ...
    
    // DRAWING SLOT
    QImage image(file);
    transformedScene->setSceneRect(image.rect());
    transformedScene->addPixmap(QPixmap::fromImage(image));
    transformedImageContainer->fitInView(scene-sceneRect(),Qt::AspectRatioMode::KeepAspectRatio);
    
    ...
    
    // ZOOM IN AND OUT SLOTS
    void window::zoomIn()
    {
        zoom_factor *= 1.25;
        view->scale(zoom_factor, zoom_factor);
        view->update();
    }
    
    void PreProcessScene::zoomOut()
    {
         zoom_factor *= 0.8;
         view->scale(zoom_factor, zoom_factor);
         view->update();
    }
    

    Do you know why do I get this strange behavior ?


Log in to reply