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. QGraphicsScene position and size

QGraphicsScene position and size

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 2.3k Views 3 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.
  • B Offline
    B Offline
    Bert59
    wrote on last edited by
    #1

    Hi,
    I have the following ui.
    f626f78c-bdef-490b-990f-b1261a335139-image.png

    The code is as follows:

        m_scene = new QGraphicsScene(this);
        m_chart = new QChart();
        ui->graphicsView->setScene(m_scene);
        m_scene->addItem(m_chart);
    

    I have reimplemented resizeEvent(QResizeEvent *event) in order to change the size of the chart when the window is resized.

    The code in resizeEvent(QResizeEvent *event) is as follows:

    m_scene->setSceneRect(QRect(QPoint(0, 0), ui->graphicsView->size()));
    m_chart->resize(ui->graphicsView->size());
    

    Resizing is working but the issue I have are the scrollbars.

    • Why are they displayed given that the Scene size is the same as the View size?

    • How can I get rid of them?

    Pl45m4P artwawA 2 Replies Last reply
    0
    • B Bert59

      Hi,
      I have the following ui.
      f626f78c-bdef-490b-990f-b1261a335139-image.png

      The code is as follows:

          m_scene = new QGraphicsScene(this);
          m_chart = new QChart();
          ui->graphicsView->setScene(m_scene);
          m_scene->addItem(m_chart);
      

      I have reimplemented resizeEvent(QResizeEvent *event) in order to change the size of the chart when the window is resized.

      The code in resizeEvent(QResizeEvent *event) is as follows:

      m_scene->setSceneRect(QRect(QPoint(0, 0), ui->graphicsView->size()));
      m_chart->resize(ui->graphicsView->size());
      

      Resizing is working but the issue I have are the scrollbars.

      • Why are they displayed given that the Scene size is the same as the View size?

      • How can I get rid of them?

      artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by artwaw
      #3

      @Bert59
      m_scene-setSeceneRect(QRect(QPoint(0,0), ui->graphicsView->viewport()->rect().size())); should be what you need.

      For more information please re-read.

      Kind Regards,
      Artur

      JonBJ 1 Reply Last reply
      2
      • B Bert59

        Hi,
        I have the following ui.
        f626f78c-bdef-490b-990f-b1261a335139-image.png

        The code is as follows:

            m_scene = new QGraphicsScene(this);
            m_chart = new QChart();
            ui->graphicsView->setScene(m_scene);
            m_scene->addItem(m_chart);
        

        I have reimplemented resizeEvent(QResizeEvent *event) in order to change the size of the chart when the window is resized.

        The code in resizeEvent(QResizeEvent *event) is as follows:

        m_scene->setSceneRect(QRect(QPoint(0, 0), ui->graphicsView->size()));
        m_chart->resize(ui->graphicsView->size());
        

        Resizing is working but the issue I have are the scrollbars.

        • Why are they displayed given that the Scene size is the same as the View size?

        • How can I get rid of them?

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

        @Bert59 said in QGraphicsScene position and size:

        Why are they displayed given that the Scene size is the same as the View size?

        Make the scene a few px smaller.


        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
        • B Bert59

          Hi,
          I have the following ui.
          f626f78c-bdef-490b-990f-b1261a335139-image.png

          The code is as follows:

              m_scene = new QGraphicsScene(this);
              m_chart = new QChart();
              ui->graphicsView->setScene(m_scene);
              m_scene->addItem(m_chart);
          

          I have reimplemented resizeEvent(QResizeEvent *event) in order to change the size of the chart when the window is resized.

          The code in resizeEvent(QResizeEvent *event) is as follows:

          m_scene->setSceneRect(QRect(QPoint(0, 0), ui->graphicsView->size()));
          m_chart->resize(ui->graphicsView->size());
          

          Resizing is working but the issue I have are the scrollbars.

          • Why are they displayed given that the Scene size is the same as the View size?

          • How can I get rid of them?

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by artwaw
          #3

          @Bert59
          m_scene-setSeceneRect(QRect(QPoint(0,0), ui->graphicsView->viewport()->rect().size())); should be what you need.

          For more information please re-read.

          Kind Regards,
          Artur

          JonBJ 1 Reply Last reply
          2
          • B Offline
            B Offline
            Bert59
            wrote on last edited by
            #4

            Thank you for your support.

            The solution of Pl45m4 is working but the solution of artwaw seems more elegant.

            Kind regards

            1 Reply Last reply
            0
            • artwawA artwaw

              @Bert59
              m_scene-setSeceneRect(QRect(QPoint(0,0), ui->graphicsView->viewport()->rect().size())); should be what you need.

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

              @artwaw
              I'm sure you're right, but I struggle with this conceptually. I have only done a couple of apps with QGraphicsScene/View.

              Why are we setting the scene rect at all, why are we trying to fit it to the view? Why aren't we setting the view size to the scene?

              artwawA 1 Reply Last reply
              0
              • JonBJ JonB

                @artwaw
                I'm sure you're right, but I struggle with this conceptually. I have only done a couple of apps with QGraphicsScene/View.

                Why are we setting the scene rect at all, why are we trying to fit it to the view? Why aren't we setting the view size to the scene?

                artwawA Offline
                artwawA Offline
                artwaw
                wrote on last edited by
                #6

                @JonB background - for me this concept (viewport, that is) dates back to Borland Pascal 7 (DOS graphic modes) & Turbo Vision 2.0 (text mode windows) so I am sorry if I didn't get things quite Qt way.

                Scene can be, and very often is, larger than viewport. Again, size of the view itself is larger than that of the viewport. Essentially it is laid out in the docs, QGraphicsView visualises the contents of a QGraphicsScene in a scrollable viewport. QGraphicsView::size() returns the size of the view with all the decorators (frames, borders, what's not) while viewport() is the space on which the scene is being visible.

                Imagine image browser written using QGraphicsView and the corresponding scene. Such program often has different modes of displaying the image (for the sake of the example we'll leave fullscreen view on the side here): fit all in window (viewport really), fit only larger images (scale down), fit only smaller images (scale up) and finally do not fit anything.
                The way to go is to render QGraphicsPixmapItem (for raster images) on the scene which is presented in the view. Simple. But.
                One of the properties of the scene is that it only grows, never shrinks on its own. So you have to clip it from time to time (for example when new image loaded is smaller in the view than the previous one, otherwise you get small image with completely unnecessary scrollbars. Now, if you clip it to the size of the view, you will see the scrollbars anyway (and sometimes the image itself will be clipped by lack of the space) since the scene will be larger than the viewport.
                Of course, with scaling single GraphicsItem we can use https://doc.qt.io/qt-5/qgraphicsview.html#fitInView-2 but still we need to take care of clipping the scene.

                All this behaviour I consider completely understandable, especially when you try to render the graphics with mobile viewport (pov), when you have many items to render from which not all need to visible (or rather in the viewport) at the given moment...

                Ok, this is longer than I intended (but I can talk about interesting things for hours, so there you have it).

                I also wrote (for my personal use, at least for now) the image browser with basic functionalities like IrfanView, since there is nothing reliable or free I could find on macOS that would sit right with me. I used QGraphicsView for the display and the journey from the idea to the working (more or less) product has been really enjoyable. So there, that's a bunch of my purely personal impressions.

                Cheers.

                For more information please re-read.

                Kind Regards,
                Artur

                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