QVideoWidget:视频被切断 [英] QVideoWidget: Video is cut off

查看:131
本文介绍了QVideoWidget:视频被切断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Qt应用程序中播放视频.到目前为止,这是我的代码:

I want to play a video in a Qt Application. This is my code so far:

#include <QApplication>
#include <QWidget>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QUrl>

#include <iostream>

using namespace std;

const int WIDTH = 1280;
const int HEIGHT = 720;

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QWidget window;
    window.resize(WIDTH, HEIGHT);
    window.setWindowTitle("Video Test");
    window.show();

    QMediaPlayer *player = new QMediaPlayer();
    player->setMedia(QUrl::fromLocalFile("/Path/To/Video.mp4"));

    QVideoWidget *videoWidget = new QVideoWidget(&window);
    player->setVideoOutput(videoWidget);

    videoWidget->resize(WIDTH, HEIGHT);

    videoWidget->show();
    player->play();


    return app.exec();
}

问题:显示视频并正常播放,但是视频的大小无法调整到适合QVideoWidget的大小.视频中比小部件大的部分被切除.

The problem: The video is shown and plays back normally, but the video does not resize to fit in the QVideoWidget. The part of the video that is bigger than the widget is cut off.

提前谢谢!

我减少了代码并注意到,当应用程序启动时,视频被切断,但是当我使用鼠标调整窗口大小时,它实际上适合大小:

I reduced the code and noticed, that when the application starts the video is cut off, but when I resize the window using the mouse it actually fits to the size:

#include <QApplication>
#include <QWidget>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QUrl>

#include <iostream>

using namespace std;

const int WIDTH = 1280;
const int HEIGHT = 720;

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QMediaPlayer *player = new QMediaPlayer();
    QVideoWidget *videoWidget = new QVideoWidget();

    player->setVideoOutput(videoWidget);

    player->setMedia(QUrl::fromLocalFile("/Path/To/Video.mp4"));
    player->play();

    videoWidget->resize(WIDTH/3, HEIGHT/3);

    videoWidget->show();

    return app.exec();
}

推荐答案

我在PyQt5中遇到了类似的问题.在播放视频之前,我通过将QVideoWidget的几何形状设置为其当前几何形状来解决该问题.我猜测resizeEvent信号中的某些内容必须处理媒体的缩放,并且在初始化时不会触发.

I ran into a similar problem in PyQt5. I worked around it by setting the geometry of the QVideoWidget to its current geometry before playing the video. I am guessing something in the resizeEvent signal must handle the scaling of the media and isn't triggered when initialized.

这篇关于QVideoWidget:视频被切断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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