QGraphicsView bigger than supposed to be (unwanted border)
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?
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);
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.
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?
The result is good, no white stripes:
was set to 680 and the displayHeight
That was a typo here, right?
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!
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.
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.
There is a quite simple, if ugly solution:
- Copy the code of "fitInView" from Qt sources into your class. Give it a new name (e.g. fitInViewTightly)
- Strip away the modifiers that cause your headache