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. QGraphicsView shows gap under scene
Forum Updated to NodeBB v4.3 + New Features

QGraphicsView shows gap under scene

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 2.3k 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.
  • W Offline
    W Offline
    Woomla
    wrote on last edited by
    #1

    When displaying a scene in a QGraphicsView that complete fits in, there's a small gap between the end of the scene and the end of the view.

    !http://i43.tinypic.com/2qss083.jpg!

    If I make the views height smaller so that the scene doesn't fit, the gap disappears:

    !http://i39.tinypic.com/5cgc1w.jpg!

    How do I get rid of this gap?

    Here's the code:

    @
    #include <QtWidgets>

    void line(QGraphicsScene &scene, int x, int y, QColor color = QColor("#9BA300"))
    {
    QGraphicsRectItem *item = scene.addRect(0, 0, 2, y, QPen(Qt::NoPen), QBrush(color));
    item->setPos(3 * x, 0);
    }

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QGraphicsView graphicsView;
    graphicsView.resize(320, 240);
    graphicsView.show();
    graphicsView.setWindowTitle(QApplication::translate("GraphicsSandbox", "Graphics sandbox"));

    graphicsView.setFrameStyle(QFrame::NoFrame);
    graphicsView.setRenderHint(QPainter::Antialiasing, true);
    graphicsView.setDragMode(QGraphicsView::NoDrag);
    graphicsView.setOptimizationFlags(QGraphicsView::DontSavePainterState);
    graphicsView.setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
    graphicsView.setAlignment(Qt::AlignBottom | Qt::AlignLeft);
    graphicsView.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    graphicsView.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    graphicsView.setTransformationAnchor(QGraphicsView::NoAnchor);

    QGraphicsScene scene;
    scene.setItemIndexMethod(QGraphicsScene::NoIndex);
    scene.setBackgroundBrush(QBrush("#242082"));
    graphicsView.setScene(&scene);

    QMatrix matrix;
    matrix.scale(1, -240/40);
    graphicsView.setMatrix(matrix);

    for (int i = 0; i < 20; ++i)
    {
    line(scene, i, 2 * i);
    }

    return app.exec();
    }
    @

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      what does the line method do?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • W Offline
        W Offline
        Woomla
        wrote on last edited by
        #3

        The function line is at the top of my code. It adds a rect and set the position. Basically it draws the green bars.

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          i know ;)
          But i wanted to see the code to check for errors there (e.g. for rounding errors etc.)

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            Can you simplify the example so that we can easily see the actual coordinates of the line that you draw, and compare it to the size of the scene?

            I believe this has to do with the way that coordinates are translated into actual pixels (turning off antialiasing might make that effect more visible).
            Basically, an integer coordinate is not exactly "a pixel", it is at the point between four pixels. Coordinates then have to be mapped to the pixels, i.e. the painter has to decide which pixels to fill, and which not to fill.
            Depending on your exact values, the visible result might not be what you expect.

            1 Reply Last reply
            0
            • W Offline
              W Offline
              Woomla
              wrote on last edited by
              #6

              (I thought I already post this :?)

              @int main(int argc, char *argv[])
              {
              QApplication app(argc, argv);

              QGraphicsView graphicsView;
              graphicsView.resize(160, 120);
              graphicsView.show();
              graphicsView.setAlignment(Qt::AlignBottom | Qt::AlignLeft);

              QGraphicsScene scene;
              graphicsView.setScene(&scene);
              scene.addRect(0, 0, 100, 100, QPen(Qt::NoPen), QBrush(QColor("black")));

              return app.exec();
              }@

              With gap on the bottom (not on the left!)
              !http://i41.tinypic.com/2w6w9yu.jpg!

              Without gap (the scene is smaller in height) than the view, so obviously no gap!)
              !http://i42.tinypic.com/177bjn.jpg!

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Asperamanca
                wrote on last edited by
                #7

                True, there are some posts missing.

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  qt-project.org servers had some troubles and it may be that data got lost.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  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