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. How to make QGraphicsItem not change size but keep the same position?
Forum Updated to NodeBB v4.3 + New Features

How to make QGraphicsItem not change size but keep the same position?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 821 Views 2 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.
  • O Offline
    O Offline
    odelaune
    wrote on last edited by
    #1

    I have a QGraphicsItem on which I add several QGraphicsItems. When the user maximise/resize the window, all QGraphicsItems are resize accordingly to keep the same aspect ratio.

    This behaviour is fine for most of my QGraphicsItems but I would like to avoid this resizing for one of them.
    On the screenshot below, the QGraphicsItem on the right (the blue sqare with the arrow inside) has the nominal size
    Capture d’écran du 2023-07-03 15-49-11.png
    Yet, when the user enlarges the window, the QGraphicsItem is stretched like the window (see below)
    Capture d’écran du 2023-07-03 15-49-33.png

    How could I force the QGraphicsItem to keep the same size (in pixel)?

    I tried to use setFlags(QGraphicsItem::ItemIgnoresTransformations); in the QGraphicsItem but doing so, it does not resize (good!) but I need to replace the QGraphicsItem because it is not located anymore on the right.

    I tried to set a new position to my QGraphicsItem after overriding resizeEvent() but I could not manage to undertand which size of the light blue area (the parent widget) I should use...

    Any hint please?

    SGaistS A 2 Replies Last reply
    0
    • JonBJ JonB

      @Asperamanca said in How to make QGraphicsItem not change size but keep the same position?:

      If that's what you want to do, you can use QGraphicsView::drawForeground (or drawBackground)

      If OP wants a static "thing" on the view, and not change size/move, that is what I would do.

      O Offline
      O Offline
      odelaune
      wrote on last edited by odelaune
      #5

      Thanks for the tip. For now I've achieved what I want by doing tricky mathematical transformations using the size of the scene in screen coordinates, the size of the scene in view coordinates and the size of the widget.

      I did not know about drawForeground() and it seems to be suitable for what I want. I will keep it in mind for next time.

      JonBJ 1 Reply Last reply
      0
      • O odelaune

        I have a QGraphicsItem on which I add several QGraphicsItems. When the user maximise/resize the window, all QGraphicsItems are resize accordingly to keep the same aspect ratio.

        This behaviour is fine for most of my QGraphicsItems but I would like to avoid this resizing for one of them.
        On the screenshot below, the QGraphicsItem on the right (the blue sqare with the arrow inside) has the nominal size
        Capture d’écran du 2023-07-03 15-49-11.png
        Yet, when the user enlarges the window, the QGraphicsItem is stretched like the window (see below)
        Capture d’écran du 2023-07-03 15-49-33.png

        How could I force the QGraphicsItem to keep the same size (in pixel)?

        I tried to use setFlags(QGraphicsItem::ItemIgnoresTransformations); in the QGraphicsItem but doing so, it does not resize (good!) but I need to replace the QGraphicsItem because it is not located anymore on the right.

        I tried to set a new position to my QGraphicsItem after overriding resizeEvent() but I could not manage to undertand which size of the light blue area (the parent widget) I should use...

        Any hint please?

        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        If memory serves well, you want to determine the position of the item using your view's coordinates and the use the mapToScene method to adjust your item position.

        Hope it helps

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • O odelaune

          I have a QGraphicsItem on which I add several QGraphicsItems. When the user maximise/resize the window, all QGraphicsItems are resize accordingly to keep the same aspect ratio.

          This behaviour is fine for most of my QGraphicsItems but I would like to avoid this resizing for one of them.
          On the screenshot below, the QGraphicsItem on the right (the blue sqare with the arrow inside) has the nominal size
          Capture d’écran du 2023-07-03 15-49-11.png
          Yet, when the user enlarges the window, the QGraphicsItem is stretched like the window (see below)
          Capture d’écran du 2023-07-03 15-49-33.png

          How could I force the QGraphicsItem to keep the same size (in pixel)?

          I tried to use setFlags(QGraphicsItem::ItemIgnoresTransformations); in the QGraphicsItem but doing so, it does not resize (good!) but I need to replace the QGraphicsItem because it is not located anymore on the right.

          I tried to set a new position to my QGraphicsItem after overriding resizeEvent() but I could not manage to undertand which size of the light blue area (the parent widget) I should use...

          Any hint please?

          A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #3

          @odelaune
          You may want to read up this: https://doc.qt.io/qt-6/graphicsview.html#the-graphics-view-coordinate-system

          In the normal order of things, GraphicsItems keep a constant size in scene coordinates (unless you resize them). What you want is an item that keeps a constant size in the view coordinate system. This is a bit tricky in principle, since GraphicsView supports multiple views on a single scene.

          If the item isn't really "part of the scene", but kind of an overlay text, you may want to consider rendering it on top of the QGraphicsView, thereby bypassing all GraphicsView coordinate systems. If that's what you want to do, you can use QGraphicsView::drawForeground (or drawBackground)

          JonBJ 1 Reply Last reply
          3
          • A Asperamanca

            @odelaune
            You may want to read up this: https://doc.qt.io/qt-6/graphicsview.html#the-graphics-view-coordinate-system

            In the normal order of things, GraphicsItems keep a constant size in scene coordinates (unless you resize them). What you want is an item that keeps a constant size in the view coordinate system. This is a bit tricky in principle, since GraphicsView supports multiple views on a single scene.

            If the item isn't really "part of the scene", but kind of an overlay text, you may want to consider rendering it on top of the QGraphicsView, thereby bypassing all GraphicsView coordinate systems. If that's what you want to do, you can use QGraphicsView::drawForeground (or drawBackground)

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #4

            @Asperamanca said in How to make QGraphicsItem not change size but keep the same position?:

            If that's what you want to do, you can use QGraphicsView::drawForeground (or drawBackground)

            If OP wants a static "thing" on the view, and not change size/move, that is what I would do.

            O 1 Reply Last reply
            0
            • JonBJ JonB

              @Asperamanca said in How to make QGraphicsItem not change size but keep the same position?:

              If that's what you want to do, you can use QGraphicsView::drawForeground (or drawBackground)

              If OP wants a static "thing" on the view, and not change size/move, that is what I would do.

              O Offline
              O Offline
              odelaune
              wrote on last edited by odelaune
              #5

              Thanks for the tip. For now I've achieved what I want by doing tricky mathematical transformations using the size of the scene in screen coordinates, the size of the scene in view coordinates and the size of the widget.

              I did not know about drawForeground() and it seems to be suitable for what I want. I will keep it in mind for next time.

              JonBJ 1 Reply Last reply
              0
              • O odelaune has marked this topic as solved on
              • O odelaune

                Thanks for the tip. For now I've achieved what I want by doing tricky mathematical transformations using the size of the scene in screen coordinates, the size of the scene in view coordinates and the size of the widget.

                I did not know about drawForeground() and it seems to be suitable for what I want. I will keep it in mind for next time.

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

                @odelaune
                Yes, drawForeground() would avoid all this! And you would not represent that "arrow" by a graphics item on the scene, it would just be artefact drawn on the view foreground. Which I suspect is just what you want here.

                1 Reply Last reply
                1

                • Login

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