Congratulations to our 2022 Qt Champions!

Resizing the widget containing a QGraphicsScene and QGraphicsView

  • Hello,

    I'm having some problems with the Graphics Framework. What I am looking to do is:

    First my 'mainwindow' is a QSplitter Widget where I set the sizes to have a mainwindow split in half. So I have in my mainWindow which is a QMainWindow, my splitter widget and in each section of the splitter I put in a QWidget inside, so basically I have something like this inside my QMainWindow:
    splitter = new QSplitter(Qt::Vertical, this);
    Then I create a QGraphicsView and a QGraphicsScene. To the scene I add a custom item which is a class derived from QGraphicsItem which paints an image.

    Now I want this image only to be displayed in the first half of my main window so basically in widget1 above which is inside the splitter. So then I do:

    QGraphicsView view = new QGraphicsView(widget1);
    QGraphicsScene* scene = new QGraphicsScene(widget1); //giving widget1 as parent which is inside the splitter.


    My questions are:

    How can I set the boundingRect of the scene to the size of the widget inside the splitter since it will get it's size according to the splitter layout, this so I can have my image resized to fit into the whole first splitter-slot?

    I'm using a QSplitter because I want to be able to resize the image I'm displaying in widget1. But my problem is that when I grab the splitter handle and move it up or down, the application freezes a few seconds (like 5 secs)and then resizes the splitter, I want to be able to resize the image I display in the splitter slot freely and fluidly like it was behaving when the splitter had nothing inside its layout.

    Could someone please stir me in the right direction to be able to accomplish this? Is there a function I need to reimplement or create for this to behave like the splitter was behaving before painting the image inside one of its 'slots'. I really appreciate the help.


  • Hello.
    Maybe you not need widget1 and instead of it just put QGraphicsView in splitter.
    If you need widget1 then put QGraphicsView to layout and set layout to widget1
    @widget1->setLayout(layout);@ or maybe I don't fully understand everything.
    Can you give us how you resize the image?
    And please use tag @ for code snippets or just click on right "Code" button in text editor, it makes the code clearer. Thanks.

  • Do not bother with Widget1, just add your QGraphicsView as a direct child of the splitter.

  • Thanks for the replies, so here is my dilemma and the reason I am using widget1 and widget2 inside the splitter:

    In fact my mainWindow layout is a splitter with a nested splitter, so First I have a Horizontal splitter that splits the screen in 2, so in the right "slot" I put another splitter which is a vertical splitter called "nestedsplitter". Nested splitter also splits the window in two(vertically) and I will display a QImage inside the top slot of the nested splitter and another image inside the bottom slot of nestedSplitter. I use splitter because I need to resize these images vertically whenever I want.

    So in my code I use a function called createWidgets() that creates all my widgets along with the splitters, and a QGraphicsView Widget that has my scene and Graphics Item. After everything is created I layout all the widgets inside the splitters.

    So when I make my QGraphicsView and scene I set the sceneRect to be the size of my QGView Widget and inside my GraphicsItem class, I pass the scene which the item will be added to, this to get the boundingRect() to return the sceneRect size which was set to be the size of the QGraphicsView widget and in the paint method I do

    painter->drawImage(sceneRect, myImage);

    With this I get the image painted inside the splitter slot that I want, but only a portion of it and not the entire top or bottom slot of my nestedSplitter mentioned above. For this is that I tried putting a widget (widget1) inside the splitter and passing that as parent to the QGraphicsView widget, and with that size I set the setSceneRect to later draw my image in the entire splitter slot. But the problem that its only painting a portion of the image is I think that the size of the parent I pass in this case widget1 in the splitter doesnt have a size until I set @nestedSplitter->addWidget@ and use the setsizes for the nestedsplitter.

    Also when I move the splitter handler to resize it is very slow and the image doesn't get resized to fit the splitter that is containing it. Instead scrollbars appear.

    So I would like to know where is it that I have to handle the resizing of the image so that when I change the size of the splitter, the image gets resized appropriately to always occupy the "slot" of the splitter that contains it, and also how can I properly set the scenerect so the whole image gets drawn and not only a portion of it even though when I pass only widget1 into the splitter it occupies the whole splitter slot as the view should.
    I would really appreciate any guidance, I hope I was clear enough :)

Log in to reply