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 bigger than supposed to be (unwanted border)

QGraphicsView bigger than supposed to be (unwanted border)

Scheduled Pinned Locked Moved Unsolved General and Desktop
qgraphicsviewqgraphicssceneqgraphics
10 Posts 3 Posters 6.8k 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.
  • Joel BodenmannJ Offline
    Joel BodenmannJ Offline
    Joel Bodenmann
    wrote on last edited by Joel Bodenmann
    #1

    I am writing a little drag'n'drop GUI designing tool for an embedded GUI library. I use QGraphicsView and QGraphicsScene to emulate the display. As the display has a fixed size (in pixels) I need the QGraphicsView to always show the entire QGraphicsScene - not more and not less. Therefore, I disabled both scrollbars of the QGraphicsView and made sure that the QGraphicsView shows the entire QGraphicsScene using this code:

    // Disable scrollbars
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    
    // Apply dimensions
    scene->setSceneRect(0, 0, displayWidth, displayHeightght);
    view->setFixedSize(displayWidth, displayHeight);
    view->fitInView(0, 0, displayWidth, displayHeight);
    

    However, when I place a rectangle that is exactly the size of the display (and therefore the scene) I can see a small border on the right and the bottom side where the view is slightly bigger than the scene. This screenshot illustrates it: http://paste.ugfx.org/sores/7505d64a54e0/3569eb5be392.jpg
    In that screenshot the displayWidth (as seen in the code above) was set to 640 and the displayHeight was set to 480. Then I placed the grey QGraphicsItem with the same dimensions at the origin (so a rectangle of 640x480 size at 0/0). The white border at the right side and the bottom is unwanted as the view should show exactly the scene from 0,0 to 640x480.

    Can anybody give me a hint on what causes this behavior and more importantly: How to fix it?

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      Hi! This works for me. Did you set the frame shape to "no frame" and the frame's line width to zero?

          const int w = 640;
          const int h = 480;
          ui->graphicsView->setFixedSize(w, h);
          QGraphicsScene *scene = new QGraphicsScene(this);
          ui->graphicsView->setScene(scene);
          scene->setSceneRect(0,0,w,h);
          QGraphicsRectItem *rect = new QGraphicsRectItem;
          rect->setRect(QRectF(0,0,w,h));
          rect->setBrush(QBrush(QColor("grey")));
         rect->setPen(QPen(Qt::NoPen));
          scene->addItem(rect);
      
      1 Reply Last reply
      0
      • Joel BodenmannJ Offline
        Joel BodenmannJ Offline
        Joel Bodenmann
        wrote on last edited by Joel Bodenmann
        #3

        I didn't modify the frame settings. For testing purposes I now disabled the frame and also set the frame width to zero as per your recommendation but that didn't change the fact that I still get white stripes at the right side and the bottom. It's only that the frame went away, but that was not my issue.
        The problem is that the view seems to be slightly bigger than the scene.

        Any other ideas?
        You mentioned that it works correctly for you. Just wondering - what Qt version are you using? Are you absolutely sure that you get no area that's not covered by the rect item that you added? Can you overwrite QGraphicsScene::drawBackground() to use a different background color in order to be just absolutely sure?

        Thank you for your help. Very appreciated.

        Edit: Did you disable the scrollbars too? Because interestingly the two stripes on the bottom and the left side are exactly where the scrollbars would be.

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by A Former User
          #4

          My system is openSUSE leap 42 on x86_64 with Mesa DRI Intel G41 graphics. Qt version is 5.5.1 (provided by openSUSE).

          I added the following to make sure the background is flaming red: scene->setBackgroundBrush(QBrush(QColor("red")));. I tested it without the grey rectangle and yes, the background is indeed red now.

          Did you disable the scrollbars too?

          Yes.

          The result is good, no white stripes:

          big
          zoom left
          zoom right

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            @Joel-Bodenmann said:

            was set to 680 and the displayHeight

            That was a typo here, right?

            Joel BodenmannJ 1 Reply Last reply
            0
            • ? A Former User

              @Joel-Bodenmann said:

              was set to 680 and the displayHeight

              That was a typo here, right?

              Joel BodenmannJ Offline
              Joel BodenmannJ Offline
              Joel Bodenmann
              wrote on last edited by Joel Bodenmann
              #6

              @Wieland said:

              That was a typo here, right?

              Yep, that was a typo indeed. I edited the post to fix it to avoid further confusion.
              I am still trying to figure out what is going on... Thanks for all your efforts. Very much appreciated!

              Industrial process automation software: https://simulton.com
              Embedded Graphics & GUI library: https://ugfx.io

              Joel BodenmannJ 1 Reply Last reply
              0
              • Joel BodenmannJ Joel Bodenmann

                @Wieland said:

                That was a typo here, right?

                Yep, that was a typo indeed. I edited the post to fix it to avoid further confusion.
                I am still trying to figure out what is going on... Thanks for all your efforts. Very much appreciated!

                Joel BodenmannJ Offline
                Joel BodenmannJ Offline
                Joel Bodenmann
                wrote on last edited by Joel Bodenmann
                #7

                @Wieland
                Sorry for the late reply, I had to take care of some exams first.

                May I ask you to retry your example but with the following modification:

                qreal scaleFactor = 1.0;
                
                setFixedSize(w*scaleFactor, h*scaleFactor);
                scale(scaleFactor, scaleFactor);
                fitInView(0, 0, w, h);
                

                I get the same result as you as long as I don't try to scale anything. As soon as I apply the code form above I get the unwanted strips at the left side and the bottom.

                Note: For my purpose I NEED to scale both the view and the scene. I understand that most use cases would ask for just scaling the scene but as my view represents a virtual display I need to scale that too.

                Industrial process automation software: https://simulton.com
                Embedded Graphics & GUI library: https://ugfx.io

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

                  You might find these interesting:
                  https://bugreports.qt.io/browse/QTBUG-1047
                  https://bugreports.qt.io/browse/QTBUG-42331
                  https://bugreports.qt.io/browse/QTBUG-11945

                  Joel BodenmannJ 1 Reply Last reply
                  0
                  • A Asperamanca

                    You might find these interesting:
                    https://bugreports.qt.io/browse/QTBUG-1047
                    https://bugreports.qt.io/browse/QTBUG-42331
                    https://bugreports.qt.io/browse/QTBUG-11945

                    Joel BodenmannJ Offline
                    Joel BodenmannJ Offline
                    Joel Bodenmann
                    wrote on last edited by
                    #9

                    @Asperamanca
                    Thank you for showing these. So this issue has been reported in 2007 and 9 years later it's still not fixed? :/
                    I tried the workarounds mentioned with calling fitInView() twice but that didn't help.

                    Industrial process automation software: https://simulton.com
                    Embedded Graphics & GUI library: https://ugfx.io

                    A 1 Reply Last reply
                    0
                    • Joel BodenmannJ Joel Bodenmann

                      @Asperamanca
                      Thank you for showing these. So this issue has been reported in 2007 and 9 years later it's still not fixed? :/
                      I tried the workarounds mentioned with calling fitInView() twice but that didn't help.

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

                      @Joel-Bodenmann

                      There is a quite simple, if ugly solution:

                      1. Copy the code of "fitInView" from Qt sources into your class. Give it a new name (e.g. fitInViewTightly)
                      2. Strip away the modifiers that cause your headache
                      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