I am developing a video player, using a QGraphicsView
to display the video. The QGraphicsView
is displaying a QGraphicsScene
with a single QGraphicsPixmapItem
which contains the current video frame. The background of the view is black.
As long as the frame is smaller than the view, everything is alright, the video frame is displayed in the center of the view and the rest of the view is black. When the view has the same size as the frame, only the frame is shown, (obviously) no background. When the video frame is greater than the view, scrollbars are shown so the user can scroll to see the other parts of the frame.
The problem: When the scrollbars are shown, it is possible to scroll past the video frame. There is a margin of 8 pixels on the bottom and on the right where the background is visible. If the video frame is greater than the view, there should be no background visible and it should not be possible to scroll past the video frame.
I reduced the problem to a short source code that demonstrates the problem, showing a 200x200 px red QPixmap
in a QGraphicsView
with green background.
#include <QtGui/QApplication>
#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsView>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow window;
QPixmap pixmap(200, 200);
pixmap.fill(QColor(255, 0, 0));
QGraphicsScene scene(&window);
scene.addPixmap(pixmap);
QGraphicsView view(&window);
view.setBackgroundBrush(QColor(0, 255, 0));
view.setScene(&scene);
window.setCentralWidget(&view);
window.show();
window.resize(199, 199);
return app.exec();
}
I've also made an image of the problem (the black border isn't included in the example code): http://imgur.com/4X5eyhC
On the left window, the QGraphicsView
has the same size as the rectangle, on the right window it is a little bit smaller, so scrollbars are shown. And also the background is visible (it should not be visible).
I already tried setting the sceneRect and various other attributes of QWidget
, QGraphicsView
and QGraphicsScene
but found nothing that changed anything with the problem.
I also tried running the example problem in a virtual machine to exclude the possibility that my Qt/KDE version has a bug.
I have no idea why the background is suddenly shown when there are scrollbars. How can I get rid of that? If that is not possible, do you have an idea how I could work around that?
Thanks in advance.
Problem is a bug with QGraphicsView::fitInView() they don't care about: https://bugreports.qt.io/browse/QTBUG-11945
Instead of reimplementing your own fitInView, just remove the margins from the view when you create it.
view.setViewportMargins(-2, -2, -2, -2)
view.setFrameStyle(QFrame.NoFrame)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With