I have the following
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();
everything works except that when I close the window for the QVideoWidget that pops up the video keeps playing somewhere, or at least the sound does. I thought that connect line would do the trick but it does not. What is the correct way to stop playback when closing the QVideoWidget window?
The problem is most likely that the window is not destroyed when you close it, so
stop
does not actually get called. You can to set theQt::WA_DeleteOnClose
using theQWidget::setAttribute()
to change the behaviour.