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. Rectangle in scene is changing it's position after multiple times closing - reopening scene window.
Forum Updated to NodeBB v4.3 + New Features

Rectangle in scene is changing it's position after multiple times closing - reopening scene window.

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 3 Posters 963 Views 1 Watching
  • 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.
  • JonBJ JonB

    @tushu said in Rectangle in scene is changing it's position after multiple times closing - reopening scene window.:

    So each time, I am adding all small rect in scene and then I am getting scene rect changed, and then I am creating outermost rect and adding it into scene.

    So in that case isn't that the answer? Like I said, when you add items to a scene outside its dimensions then scene rectangle will grow to encompass. If you want to analyse the exact behaviour and claim it is wrong/you cannot understand it, then please provide a minimal example (not your current code) illustrating the issue.

    T Offline
    T Offline
    tushu
    wrote on last edited by
    #5

    @JonB
    I am confused.
    I am adding objects in scene , then
    I am adding outermost rectangle , then
    I am closing scene window

    Now I am opening scene window again,
    and above things will happen again.

    Then Why scene rectangle will grow ?
    scene has been destroyed and opening freshly again.

    JonBJ 1 Reply Last reply
    0
    • T tushu

      @JonB
      I am confused.
      I am adding objects in scene , then
      I am adding outermost rectangle , then
      I am closing scene window

      Now I am opening scene window again,
      and above things will happen again.

      Then Why scene rectangle will grow ?
      scene has been destroyed and opening freshly again.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #6

      @tushu
      Like I said, a minimal example. I can only say that I have used QGraphicsScene, including closing and opening windows, and have not noticed anything untoward in the sceneRect() geometry. QGraphicsScene does not have any visuals or "window" (only QGraphicsView has that). Don't even know from your description whether you retain the existing QGraphicsScene and its content or recreate and repopulate again. Like I said, a minimal example.....

      Meanwhile you might find calling QRectF QGraphicsScene::itemsBoundingRect() const at various stages useful to understand what is going on.

      1 Reply Last reply
      0
      • T tushu

        @JonB

        So here I want to tell you, I am adding 5 small rectangles and lines etc and then I am adding outermost rectangle in it.

        I tried the signal you suggested. It is showing following output.

        // here I am adding all small rectangle and lines
        // qDebug output of signal you suggested
        Scene rect changed
        // here I am adding outermost rect and printing its co-oridnates.
        top left --> ( 9 , -62 )
        bottom right --> (208 , 149)

        So each time, I am adding all small rect in scene and then I am getting scene rect changed, and then I am creating outermost rect and adding it into scene.

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #7

        @tushu

        You could try to set a "static" sceneRect, so you start with the same setup every time.

        // topleft is (0/0) now
        _scene->setSceneRect(0, 0, graphicsView->width(), graphicsView->height());
        // Then add your items
        

        Then you also dont have to deal with negative coodinates, unless you want to move or zoom.


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

        ~E. W. Dijkstra

        JonBJ 1 Reply Last reply
        0
        • Pl45m4P Pl45m4

          @tushu

          You could try to set a "static" sceneRect, so you start with the same setup every time.

          // topleft is (0/0) now
          _scene->setSceneRect(0, 0, graphicsView->width(), graphicsView->height());
          // Then add your items
          

          Then you also dont have to deal with negative coodinates, unless you want to move or zoom.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #8

          @Pl45m4
          @tushu's output shows that the scene's size is changing (as well its top left coordinates). I don't see that as related to whether coordinates are negative or positive? Plus even if you initialise the scene rect to the view's size, that won't be relevant if OP adds new objects which change the scene size.

          Now, if he is already using something like setSceneRect(0, 0, graphicsView->width(), graphicsView->height()); that would initialize the scene size to that of the view, and I don't know whether the view's size might be changing as he closes and re-opens the parent window, which could lead to scene size changes with such code.

          T Pl45m4P 2 Replies Last reply
          0
          • JonBJ JonB

            @Pl45m4
            @tushu's output shows that the scene's size is changing (as well its top left coordinates). I don't see that as related to whether coordinates are negative or positive? Plus even if you initialise the scene rect to the view's size, that won't be relevant if OP adds new objects which change the scene size.

            Now, if he is already using something like setSceneRect(0, 0, graphicsView->width(), graphicsView->height()); that would initialize the scene size to that of the view, and I don't know whether the view's size might be changing as he closes and re-opens the parent window, which could lead to scene size changes with such code.

            T Offline
            T Offline
            tushu
            wrote on last edited by
            #9

            @JonB , @Pl45m4

            I randomly tried this and it worked.

            In my creation of outermost rectangle code , there is a line,

            QRectF Rect(topLeftX /*+ 3*/, topLeftY /*- 20*/, bottomRightX /*+ 10*/, bottomRightY /*+ 30*/);
            

            Here if I removed +3, -20 +10 and +30 , then my problem gets solved.

            But not understanding why it got solved ? What is the reason ?

            I had added those numbers to adjust outermost rectangle's width and height.
            ( Now it's width has reduced little bit, which does not look good. )

            Please throw some light on this fixation.

            JonBJ 1 Reply Last reply
            0
            • JonBJ JonB

              @Pl45m4
              @tushu's output shows that the scene's size is changing (as well its top left coordinates). I don't see that as related to whether coordinates are negative or positive? Plus even if you initialise the scene rect to the view's size, that won't be relevant if OP adds new objects which change the scene size.

              Now, if he is already using something like setSceneRect(0, 0, graphicsView->width(), graphicsView->height()); that would initialize the scene size to that of the view, and I don't know whether the view's size might be changing as he closes and re-opens the parent window, which could lead to scene size changes with such code.

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #10

              @JonB

              The scene rectangle defines the extent of the scene. It is primarily used by QGraphicsView to determine the view's default scrollable area, and by QGraphicsScene to manage item indexing.

              If unset, or if set to a null QRectF, sceneRect() will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks).

              (https://doc.qt.io/qt-6/qgraphicsscene.html#sceneRect-prop)

              @tushu are you really destroying everything? Then the output should be the same.
              If not, you add your items to your existing scene, which already was altered according to your items which were added before.

              I also helps to work with the view dimensions you will get in/after showEvent as they are more precise (due to layouts etc.) than the one in your c'tor.


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

              ~E. W. Dijkstra

              1 Reply Last reply
              1
              • T tushu

                @JonB , @Pl45m4

                I randomly tried this and it worked.

                In my creation of outermost rectangle code , there is a line,

                QRectF Rect(topLeftX /*+ 3*/, topLeftY /*- 20*/, bottomRightX /*+ 10*/, bottomRightY /*+ 30*/);
                

                Here if I removed +3, -20 +10 and +30 , then my problem gets solved.

                But not understanding why it got solved ? What is the reason ?

                I had added those numbers to adjust outermost rectangle's width and height.
                ( Now it's width has reduced little bit, which does not look good. )

                Please throw some light on this fixation.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #11

                @tushu
                But you don't tell me where you get the topLeftX, topLeftY , ... paramters from. If you are saying those are something like the current scene rectangle values then this rectangle is moved from the initial position and has a greater size, doesn't it? So if you then use this e.g. to create a QGraphicsRectItem which you then add to the scene, as I said the scene's geometry will change to accommodate it. So is this your explanation?

                Note that if you are only adding this to "make something look nice", you might be better doing some drawing in the QGraphicsView renderer instead. Don't add objects to a graphics scene which are not "real" objects for your graphics model.

                T 1 Reply Last reply
                1
                • JonBJ JonB

                  @tushu
                  But you don't tell me where you get the topLeftX, topLeftY , ... paramters from. If you are saying those are something like the current scene rectangle values then this rectangle is moved from the initial position and has a greater size, doesn't it? So if you then use this e.g. to create a QGraphicsRectItem which you then add to the scene, as I said the scene's geometry will change to accommodate it. So is this your explanation?

                  Note that if you are only adding this to "make something look nice", you might be better doing some drawing in the QGraphicsView renderer instead. Don't add objects to a graphics scene which are not "real" objects for your graphics model.

                  T Offline
                  T Offline
                  tushu
                  wrote on last edited by
                  #12

                  @JonB
                  Yes, your explanation is perfect. I accept it.

                  @Pl45m4
                  You are right, I am not destroying everything.
                  I am not deleting _scene, after closing scene. I am clearing all the objects in it but not deleting scene.

                  JonBJ Pl45m4P 2 Replies Last reply
                  0
                  • T tushu

                    @JonB
                    Yes, your explanation is perfect. I accept it.

                    @Pl45m4
                    You are right, I am not destroying everything.
                    I am not deleting _scene, after closing scene. I am clearing all the objects in it but not deleting scene.

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by JonB
                    #13

                    @tushu said in Rectangle in scene is changing it's position after multiple times closing - reopening scene window.:

                    I am not deleting _scene, after closing scene. I am clearing all the objects in it but not deleting scene.

                    As gfx items get added to a scene the scene grows to accomodate them, if necessary. However, it does not shrink when you remove or clear them. This is explained in https://doc.qt.io/qt-6/qgraphicsscene.html#sceneRect-prop

                    If unset, or if set to a null QRectF, sceneRect() will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks).

                    At minimum you should perhaps do a setSceneRect(QRectF()), but I think this actually limits the scene permanently and then adding items does not grow it. So not what you want. Yes, this might work to reset the size, you could try it.

                    1 Reply Last reply
                    0
                    • T tushu

                      @JonB
                      Yes, your explanation is perfect. I accept it.

                      @Pl45m4
                      You are right, I am not destroying everything.
                      I am not deleting _scene, after closing scene. I am clearing all the objects in it but not deleting scene.

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on last edited by
                      #14

                      @tushu said in Rectangle in scene is changing it's position after multiple times closing - reopening scene window.:

                      You are right, I am not destroying everything.
                      I am not deleting _scene, after closing scene. I am clearing all the objects in it but not deleting scene.

                      Yes, that's what I wrote here.
                      With your additional movement in negative direction here

                      QRectF Rect(topLeftX /+ 3/, topLeftY /- 20/, bottomRightX /+ 10/, bottomRightY /+ 30/);

                      you expand your scene by 20. And it stays like this as long as you dont destroy your scene. When you add your items again, your items are not centered in your viewport anymore


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

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      0

                      • Login

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