QVideoWidget 独立。窗口关闭时如何停止视频?

Posted

技术标签:

【中文标题】QVideoWidget 独立。窗口关闭时如何停止视频?【英文标题】:QVideoWidget stand alone. How to stop video when window closes? 【发布时间】: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窗口时停止播放的正确方法是什么?

【问题讨论】:

【参考方案1】:

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

vw->setAttribute( Qt::WA_DeleteOnClose );

【讨论】:

这在我用 vw 而不是 vw->parent() 更正我的连接调用后工作了

以上是关于QVideoWidget 独立。窗口关闭时如何停止视频?的主要内容,如果未能解决你的问题,请参考以下文章

窗口关闭时停止模式(可可)

弹出窗口关闭时停止 youtube 视频 [重复]

如何在不关闭 GUI 窗口的情况下停止运行 PyQt5 程序?

PyQt - 如何在不关闭对话框窗口的情况下停止执行

在窗口关闭时停止异步操作

linux如何关闭ssh会话不停止程序运行 screen