【问题标题】:QVideoWidget stand alone. How to stop video when window closes?QVideoWidget 独立。窗口关闭时如何停止视频?
【发布时间】:2016-02-15 19:05:22
【问题描述】:

我有以下

QVideoWidget* vw = new QVideoWidget;
QMediaResource mr(QUrl::fromLocalFile(item->data(Qt::UserRole + FilepathRole).toString()));
QMediaContent mc(mr);
QMediaPlayer* player = new QMediaPlayer;
QObject::connect(vw,SIGNAL(destroyed(QObject*)),player,SLOT(stop()));
player->setMedia(mc);
player->setVideoOutput(vw);
QRect rect = QApplication::desktop()->availableGeometry();
int width = vids[vids.indexOf(item->data(Qt::UserRole + FilepathRole).toString())].width;
int height = vids[vids.indexOf(item->data(Qt::UserRole + FilepathRole).toString())].height;
int x = (rect.width() / 2) - (width / 2);
int y = (rect.height() / 2) - (height / 2);
vw->setGeometry(x,y,width,height);
vw->show();
player->play();

一切正常,除了当我关闭弹出的 QVideoWidget 窗口时,视频会在某处继续播放,或者至少声音会继续播放。我认为连接线可以解决问题,但事实并非如此。关闭QVideoWidget窗口时停止播放的正确方法是什么?

【问题讨论】:

    标签: c++ qt5.5


    【解决方案1】:

    问题很可能是当你关闭它时窗口没有被破坏,所以stop实际上并没有被调用。您可以使用QWidget::setAttribute() 设置Qt::WA_DeleteOnClose 来更改行为。

    vw->setAttribute( Qt::WA_DeleteOnClose );
    

    【讨论】:

    • 这在我用 vw 而不是 vw->parent() 更正我的连接调用后工作了
    猜你喜欢
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    相关资源
    最近更新 更多