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

QVideoWidget stand alone. How to stop video when window closes?

我有以下

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 的 window 时,视频会继续在某处播放,或者至少声音会继续播放。我认为连接线可以解决问题,但事实并非如此。关闭QVideoWidget时停止播放的正确方法是什么window?

问题很可能是 window 在关闭时没有被销毁,所以 stop 实际上没有被调用。您可以设置 Qt::WA_DeleteOnClose using the QWidget::setAttribute() 来改变行为。

vw->setAttribute( Qt::WA_DeleteOnClose );