Important: Please read the Qt Code of Conduct -

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

  • 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;
        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);

    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?

  • just had to add

    vw->setAttribute( Qt::WA_DeleteOnClose );

Log in to reply