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

时间:2016-02-15 19:05:23

标签: c++ qt5.5

我有以下

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 个答案:

答案 0 :(得分:1)

问题很可能在关闭窗口时不会破坏窗口,因此实际上不会调用stop。您可以使用Qt::WA_DeleteOnClose设置QWidget::setAttribute()来更改行为。

vw->setAttribute( Qt::WA_DeleteOnClose );