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

QGraphicsView size updating



  • Hi everyone !

    I am facing a little problem with QGraphicsView and QStackedWidgets. I have created multiple views that are stored in a QStackedWidget. The problem is when I try to draw something in all these views and to make it fit the view rect, only the view that is above the stack is displayed correctly, the other views display at much smaller scales. Then, if I want the scale to adjust on a view, I need to show the view by moving it on top of the stack and refresh the scene.

    In fact, I checked the size property of all my QGraphicsView, and only the first (above) is correct, the other ones are very small like if the widget hasn't set it's size yet.

    I suspect the QStackWidget to be the problem... Do you have any idea of how to overcome this problem ? I've already tried all the classic methods like update(), setGeometry ... but it still doesn't works.

    Thanks !



  • @HB76

    I guess without seeing any code, nobody can help you :)

    How many scenes do you use? Did you set a sceneRect for each scene?



  • @Pl45m4 I use 5 QGraphicsViews with one QGraphicsScene for each one. All the views are distributed in differents workshops (that's why I didn't post code because I should have posted it at all). And sometimes in a same workshop it can have multiple View with a stack. All the workshops are also stored in a stack that is used as centralWidget in the mainWindow.

    I have also inherited the QGraphicsView class to have mouse click and mouve event but that's noot very relevant since the problem was already there before when I was using simple QGraphicsViews.

    Here is some code of one workshop :

        // Constructor
        viewerStack = new QStackedWidget(this);
        viewerStack->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
        scene1 = new QGraphicsScene(this);
        scene2 = new QGraphicsScene(this);
        view1 = new GraphicsView(scene1,viewerStack);
        view1->setDragMode(QGraphicsView::ScrollHandDrag);
        view2 = new GraphicsView(scene2,viewerStack);
        view2->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
        view2->setDragMode(QGraphicsView::ScrollHandDrag);
        viewerStack->addWidget(view1);
        viewerStack->addWidget(view2);
    
    
    void workshop::renderScene(int index)
    {
        scene1->clear();
        scene2->clear();
    
        if (!image_files->isEmpty() && index<image_files->count())
        {
            QImage image(image_files->value(index));
            scene1->addPixmap(QPixmap::fromImage(image));
            if (parameters.transform)
            view1->fitInView(scene1->sceneRect(),Qt::AspectRatioMode::KeepAspectRatio);
    
            QImage processedImage = ImageProcessingToolBox::processImage(image,parameters);
            scene2->setSceneRect(processedImage.rect());
            scene2->addPixmap(QPixmap::fromImage(processedImage));
            view2->fitInView(scene2->sceneRect(),Qt::AspectRatioMode::KeepAspectRatio);
        }
    }
    


  • @HB76

    Does the same problem occur, if you use all views next to each other (without the StackedWidget)?



  • @Pl45m4 the problem does not appear if before drawing on each view, I show it (even empty) on screen. So yes if they are all next to each other and displayed on screen it works.

    That makes me think that there should be something with the initialisation of the QGraphicsView, like if the fact of storing it in stacks avoids them to initialise there geometry if they are not on top of the stack (probably because when they are on top it must create a showEvent and then adapt the size from the viewPort, which is actually the stack).


Log in to reply