Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. sceneRect() not updated immediately after window resizing

sceneRect() not updated immediately after window resizing

Scheduled Pinned Locked Moved Solved General and Desktop
scenerectqgraphicsview
9 Posts 3 Posters 799 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    odelaune
    wrote on 25 Jan 2024, 09:09 last edited by odelaune
    #1

    Hi,
    when sceneRect() is updated and how to force it to update?

    I have this piece of code in a class derived from QGraphicsView

    void DisplayAMapGraphicsView::resizeEvent(QResizeEvent *event)
    {
        QGraphicsView::resizeEvent(event);
        qDebug() << Q_FUNC_INFO << "old size:" << event->oldSize() << ", new size:" << event->size();
        qDebug() << Q_FUNC_INFO << "rect:" << rect() << ", scene rect:" << sceneRect();
    }
    

    When my software start, it has a default size, so this is displayed:

    virtual void MyGraphicsView::resizeEvent(QResizeEvent*) old size: QSize(-1, -1) , new size: QSize(400, 582)
    virtual void MyGraphicsView::resizeEvent(QResizeEvent*) rect: QRect(0,0 400x582) , scene rect: QRectF(-208,-299 400x582)

    This is correct.

    Now if I maximize the window, this is what I get

    virtual void MyGraphicsView::resizeEvent(QResizeEvent*) old size: QSize(400, 582) , new size: QSize(1400, 1002)
    virtual void MyGraphicsView::resizeEvent(QResizeEvent*) rect: QRect(0,0 1400x1002) , scene rect: QRectF(752,306 400x582)

    The size of rect is correct, but the size of sceneRect is not. It should be (1400x1002) as for rect. It is not updated.

    It is not clear to me when sceneRect() is updated.

    Do you know how to force sceneRect to update?

    P 1 Reply Last reply 25 Jan 2024, 11:34
    0
    • O odelaune
      26 Jan 2024, 08:26

      I think this is not the same problem. Here, scenereRect is not updated automatically after resizing while other rect is.

      scenereRect is updated later but I do not know when, and do not understand what triggers the enw sceneRect calculation. I will try to make a POC to demonstrate better this behaviour.

      J Offline
      J Offline
      JonB
      wrote on 26 Jan 2024, 08:37 last edited by JonB
      #8

      @odelaune
      I may be mistaken, but I don't think so. What makes you think anything would change the size of the scene rectangle after any kind of resizing (e.g. from window size change) of the view? The view refers to what you see visually, so that should resize. But the scene is like an abstract "canvas" which you draw onto, and the view is just a window of arbitrary size onto it --- it might be smaller, equal or larger than the scene, and that's fine. And btw you can have multiple views onto the same scene, one more reason why scene size would not be tied to view size.

      The only thing which resizes the scene (other than manually) is whenever you add an item to it Qt code finds the minimum bounding rectangle of all items on the scene, taking into account the newly added one. If that is larger than the current scene it enlarges the scene rectangle to encompass; if it is smaller (e.g. after removing/resizing an item) it does not reduce the scene. This is documented. If for some reason you want to enlarge the abstract scene backing canvas to correspond to view changes (don't know why you would) then I think you must do this yourself manually in view size change events.

      O 1 Reply Last reply 26 Jan 2024, 08:56
      2
      • O odelaune
        25 Jan 2024, 09:09

        Hi,
        when sceneRect() is updated and how to force it to update?

        I have this piece of code in a class derived from QGraphicsView

        void DisplayAMapGraphicsView::resizeEvent(QResizeEvent *event)
        {
            QGraphicsView::resizeEvent(event);
            qDebug() << Q_FUNC_INFO << "old size:" << event->oldSize() << ", new size:" << event->size();
            qDebug() << Q_FUNC_INFO << "rect:" << rect() << ", scene rect:" << sceneRect();
        }
        

        When my software start, it has a default size, so this is displayed:

        virtual void MyGraphicsView::resizeEvent(QResizeEvent*) old size: QSize(-1, -1) , new size: QSize(400, 582)
        virtual void MyGraphicsView::resizeEvent(QResizeEvent*) rect: QRect(0,0 400x582) , scene rect: QRectF(-208,-299 400x582)

        This is correct.

        Now if I maximize the window, this is what I get

        virtual void MyGraphicsView::resizeEvent(QResizeEvent*) old size: QSize(400, 582) , new size: QSize(1400, 1002)
        virtual void MyGraphicsView::resizeEvent(QResizeEvent*) rect: QRect(0,0 1400x1002) , scene rect: QRectF(752,306 400x582)

        The size of rect is correct, but the size of sceneRect is not. It should be (1400x1002) as for rect. It is not updated.

        It is not clear to me when sceneRect() is updated.

        Do you know how to force sceneRect to update?

        P Offline
        P Offline
        Pl45m4
        wrote on 25 Jan 2024, 11:34 last edited by
        #2

        @odelaune said in sceneRect() not updated immediately after window resizing:

        The size of rect is correct, but the size of sceneRect is not. It should be (1400x1002) as for rect. It is not updated.

        To judge if it's correct or not show your scene setup please.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        O 1 Reply Last reply 25 Jan 2024, 11:44
        0
        • P Pl45m4
          25 Jan 2024, 11:34

          @odelaune said in sceneRect() not updated immediately after window resizing:

          The size of rect is correct, but the size of sceneRect is not. It should be (1400x1002) as for rect. It is not updated.

          To judge if it's correct or not show your scene setup please.

          O Offline
          O Offline
          odelaune
          wrote on 25 Jan 2024, 11:44 last edited by
          #3

          @Pl45m4
          I have no special settings for my QGraphicsScene, but my QGraphicsView has this settings, I.e. all default settings.

          view->setRenderHint(QPainter::Antialiasing, true);
          view->setRenderHint(QPainter::TextAntialiasing, true);
          view->setRenderHint(QPainter::SmoothPixmapTransform, true);
          view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
          view->setFrameStyle(QFrame::NoFrame);
          
          view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
          view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
          view->setAttribute(Qt::WA_TranslucentBackground, true);
          view->setMouseTracking(true);
          

          Any idea?

          P 1 Reply Last reply 25 Jan 2024, 12:19
          0
          • O odelaune
            25 Jan 2024, 11:44

            @Pl45m4
            I have no special settings for my QGraphicsScene, but my QGraphicsView has this settings, I.e. all default settings.

            view->setRenderHint(QPainter::Antialiasing, true);
            view->setRenderHint(QPainter::TextAntialiasing, true);
            view->setRenderHint(QPainter::SmoothPixmapTransform, true);
            view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
            view->setFrameStyle(QFrame::NoFrame);
            
            view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            view->setAttribute(Qt::WA_TranslucentBackground, true);
            view->setMouseTracking(true);
            

            Any idea?

            P Offline
            P Offline
            Pl45m4
            wrote on 25 Jan 2024, 12:19 last edited by Pl45m4
            #4

            @odelaune said in sceneRect() not updated immediately after window resizing:

            view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);

            This is not the default setting. You could try to comment that line and check again.

            • https://doc.qt.io/qt-6/qgraphicsview.html#viewportUpdateMode-prop

            How do you set your scene to your view? Does the scene have the view as parent?

            Read through this topic. Maybe you find your answer there as we cant see everything related in your project right now.

            Edit:

            Is it only the fullScreen / maximize behavior or does any resize output a smaller sceneRect?


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            O 1 Reply Last reply 25 Jan 2024, 12:52
            0
            • P Pl45m4
              25 Jan 2024, 12:19

              @odelaune said in sceneRect() not updated immediately after window resizing:

              view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);

              This is not the default setting. You could try to comment that line and check again.

              • https://doc.qt.io/qt-6/qgraphicsview.html#viewportUpdateMode-prop

              How do you set your scene to your view? Does the scene have the view as parent?

              Read through this topic. Maybe you find your answer there as we cant see everything related in your project right now.

              Edit:

              Is it only the fullScreen / maximize behavior or does any resize output a smaller sceneRect?

              O Offline
              O Offline
              odelaune
              wrote on 25 Jan 2024, 12:52 last edited by odelaune
              #5

              @Pl45m4

              Commenting view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); does not change anything for this topic.
              I also tried to comment all lines that modify the QGraphicsView, but with no result (the sceneRect is still the old one).

              Here is how is defined the QGraphicsScene

              scene = new QGraphicsScene(this); // "this" is a QWidget
              view->setScene(scene);
              
              

              sceneRect is always smaller just after resizing, not only for full screen or maximized window.

              P 1 Reply Last reply 25 Jan 2024, 13:36
              0
              • O odelaune
                25 Jan 2024, 12:52

                @Pl45m4

                Commenting view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); does not change anything for this topic.
                I also tried to comment all lines that modify the QGraphicsView, but with no result (the sceneRect is still the old one).

                Here is how is defined the QGraphicsScene

                scene = new QGraphicsScene(this); // "this" is a QWidget
                view->setScene(scene);
                
                

                sceneRect is always smaller just after resizing, not only for full screen or maximized window.

                P Offline
                P Offline
                Pl45m4
                wrote on 25 Jan 2024, 13:36 last edited by Pl45m4
                #6

                @odelaune

                What about view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio) as @JonB wrote here?

                Edit: Just realised that this is also your topic and you marked it "solved". So haven't you found a solution back then?!


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                O 1 Reply Last reply 26 Jan 2024, 08:26
                0
                • P Pl45m4
                  25 Jan 2024, 13:36

                  @odelaune

                  What about view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio) as @JonB wrote here?

                  Edit: Just realised that this is also your topic and you marked it "solved". So haven't you found a solution back then?!

                  O Offline
                  O Offline
                  odelaune
                  wrote on 26 Jan 2024, 08:26 last edited by
                  #7

                  I think this is not the same problem. Here, scenereRect is not updated automatically after resizing while other rect is.

                  scenereRect is updated later but I do not know when, and do not understand what triggers the enw sceneRect calculation. I will try to make a POC to demonstrate better this behaviour.

                  J 1 Reply Last reply 26 Jan 2024, 08:37
                  0
                  • O odelaune
                    26 Jan 2024, 08:26

                    I think this is not the same problem. Here, scenereRect is not updated automatically after resizing while other rect is.

                    scenereRect is updated later but I do not know when, and do not understand what triggers the enw sceneRect calculation. I will try to make a POC to demonstrate better this behaviour.

                    J Offline
                    J Offline
                    JonB
                    wrote on 26 Jan 2024, 08:37 last edited by JonB
                    #8

                    @odelaune
                    I may be mistaken, but I don't think so. What makes you think anything would change the size of the scene rectangle after any kind of resizing (e.g. from window size change) of the view? The view refers to what you see visually, so that should resize. But the scene is like an abstract "canvas" which you draw onto, and the view is just a window of arbitrary size onto it --- it might be smaller, equal or larger than the scene, and that's fine. And btw you can have multiple views onto the same scene, one more reason why scene size would not be tied to view size.

                    The only thing which resizes the scene (other than manually) is whenever you add an item to it Qt code finds the minimum bounding rectangle of all items on the scene, taking into account the newly added one. If that is larger than the current scene it enlarges the scene rectangle to encompass; if it is smaller (e.g. after removing/resizing an item) it does not reduce the scene. This is documented. If for some reason you want to enlarge the abstract scene backing canvas to correspond to view changes (don't know why you would) then I think you must do this yourself manually in view size change events.

                    O 1 Reply Last reply 26 Jan 2024, 08:56
                    2
                    • J JonB
                      26 Jan 2024, 08:37

                      @odelaune
                      I may be mistaken, but I don't think so. What makes you think anything would change the size of the scene rectangle after any kind of resizing (e.g. from window size change) of the view? The view refers to what you see visually, so that should resize. But the scene is like an abstract "canvas" which you draw onto, and the view is just a window of arbitrary size onto it --- it might be smaller, equal or larger than the scene, and that's fine. And btw you can have multiple views onto the same scene, one more reason why scene size would not be tied to view size.

                      The only thing which resizes the scene (other than manually) is whenever you add an item to it Qt code finds the minimum bounding rectangle of all items on the scene, taking into account the newly added one. If that is larger than the current scene it enlarges the scene rectangle to encompass; if it is smaller (e.g. after removing/resizing an item) it does not reduce the scene. This is documented. If for some reason you want to enlarge the abstract scene backing canvas to correspond to view changes (don't know why you would) then I think you must do this yourself manually in view size change events.

                      O Offline
                      O Offline
                      odelaune
                      wrote on 26 Jan 2024, 08:56 last edited by odelaune
                      #9

                      @JonB

                      You are absolutely right! I was confused between the QGraphicsView and the QGraphicsScene. I just realised that when I was playing with my POC.

                      Actually, what change the size of the QGraphicsScene is done in another part of my code and is called later. If I recalculate the sceneRect according to my needs and call setSceneRect(), when my window is resized, everything works as expected.

                      Sorry for the inconvenience.

                      1 Reply Last reply
                      2
                      • O odelaune has marked this topic as solved on 26 Jan 2024, 08:57

                      8/9

                      26 Jan 2024, 08:37

                      • Login

                      • Login or register to search.
                      8 out of 9
                      • First post
                        8/9
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved