简体   繁体   English

QVideoWidget的尺寸调整不佳

[英]QVideoWidget doesn't resize well

I have a Qt application which simply captures from the default webcam and shows it on a QVideoWidget. 我有一个Qt应用程序,它可以简单地从默认摄像头捕获并显示在QVideoWidget上。 In the ui, I have a simple MainWindow with a QGraphicsView inside a VerticalLayout: 在用户界面中,我有一个简单的MainWindow,在VerticalLayout中带有QGraphicsView:

ui design ui设计

My mainwindow.cpp============================================= 我的mainwindow.cpp ==============================================

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_viewfinder = new QVideoWidget(ui->captureView);
    m_camera = new QCamera(QCameraInfo::defaultCamera());
    m_camera->setViewfinder(m_viewfinder);

    m_camera->start();
}

MainWindow::~MainWindow()
{
    m_camera->stop();
    delete m_viewfinder;
    delete m_camera;
    delete ui;
}

When I execute this, I get the application running, but the video contents do not scale according to the mainwindow size. 当我执行此操作时,我使应用程序运行,但是视频内容不会根据主窗口的大小缩放。 Examples: 例子:

When I start the application 当我启动应用程序时

Resizing mainwindow down 调整主窗口的大小

Resizing mainwindow up 调整主窗口大小

Is there some way to make the video content resize well and fit the available area? 有什么方法可以使视频内容调整大小并适合可用区域? I have seen this answer: QVideoWidget: Video is cut off , but it doesn't offer any solution that works for me. 我已经看到了这个答案: QVideoWidget:视频已被切断 ,但是它没有提供任何适合我的解决方案。 When use the QGraphicsView-QGraphicsScene-QGraphicsVideoItem chain, I see nothing at all. 当使用QGraphicsView-QGraphicsScene-QGraphicsVideoItem链时,我什么也看不到。

When you use the following instruction: 当您使用以下说明时:

m_viewfinder = new QVideoWidget(ui->captureView);

You are setting as the parent of m_viewfinder to captureView , so the positions of m_viewfinder will be relative to captureView , but this does not indicate that it will be the same size as the parent. 要设置为父m_viewfindercaptureView ,这样的位置m_viewfinder会相对captureView ,但这并不表明,这将是大小父一样。

One of the easiest ways to do this is to use a layout. 最简单的方法之一就是使用布局。 Also, it is not necessary to create the QGraphicsWidget or the QVBoxLayout , so I recommend you to delete it and get the design as it was established by default: 另外,也不必创建QGraphicsWidgetQVBoxLayout ,因此建议您删除它并获取默认情况下建立的设计:

在此处输入图片说明

and then we establish a layout that is placed in the centralWidget , and in this layout we add the QVideoWidget . 然后我们建立一个放置在centralWidget中的布局,并在该布局中添加QVideoWidget

...
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_viewfinder = new QVideoWidget;
    QVBoxLayout *lay = new QVBoxLayout(ui->centralWidget);
    lay->addWidget(m_viewfinder);
    m_camera = new QCamera(QCameraInfo::defaultCamera());
    m_camera->setViewfinder(m_viewfinder);

    m_camera->start();
}
...

In the following link you can find the complete example. 在下面的链接中,您可以找到完整的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM