带有滚动条的QGraphicsView内部不需要的边距 [英] Unwanted margin inside QGraphicsView with Scrollbars

查看:64
本文介绍了带有滚动条的QGraphicsView内部不需要的边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发视频播放器,使用 QGraphicsView 来显示视频. QGraphicsView 正在显示带有单个 QGraphicsPixmapItem QGraphicsScene ,其中包含当前视频帧.视图的背景是黑色的.

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.

问题:显示滚动条时,可以滚动到视频帧上方.可见背景的底部和右侧有8个像素的边距.如果视频帧大于视图,则应该没有可见的背景,并且不能滚动到视频帧之外.

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.

我将问题简化为一个简短的源代码来演示该问题,并在带有绿色背景的 QGraphicsView 中显示了一个200x200 px的红色 QPixmap .

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();
}

我也制作了该问题的图片(示例代码中未包括黑色边框): http://imgur.com/4X5eyhC

I've also made an image of the problem (the black border isn't included in the example code): http://imgur.com/4X5eyhC

在左侧窗口中, QGraphicsView 具有与矩形相同的大小,在右侧窗口中,它稍小一些,因此显示了滚动条.而且背景是可见的(它不应该是可见的).

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).

我已经尝试设置sceneRect以及 QWidget QGraphicsView QGraphicsScene 的其他各种属性,但是发现没有任何问题可以解决.

I already tried setting the sceneRect and various other attributes of QWidget, QGraphicsView and QGraphicsScene but found nothing that changed anything with the problem.

我还尝试在虚拟机中运行示例问题,以排除Qt/KDE版本存在错误的可能性.

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?

谢谢.

推荐答案

问题是他们不关心的QGraphicsView :: fitInView()问题:

Problem is a bug with QGraphicsView::fitInView() they don't care about: https://bugreports.qt.io/browse/QTBUG-11945

代替重新实现自己的fitInView,只需在创建视图时从视图中删除边距即可.

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)

这篇关于带有滚动条的QGraphicsView内部不需要的边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆