listView click start mp3 or video with default player (Windows)?



  • I try to figure out how to start a music or video file with the system default player installed on Windows. From the command line i can do that like

    start "" "c:\path to my\video file.avi"
    

    But it won't work from inside Qt. Well, at least not for me. Here is my code:

    void MainWindow::on_listView1_doubleClicked(const QModelIndex &index)
    {
        QString file = filemodel->data(index, Qt::DisplayRole).toString();
        QString startMe = "C:/player/vlc.exe \"" + dirmodel->rootPath() + "/" + file + "\"";
        QProcess *startVideo = new QProcess();
        startVideo->start(startMe);
    }
    

    The code shows my successful attempt to start it with VLC. And this one was my system call.

    QString startMe = "start \"""\" \"" + dirmodel->rootPath() + "/" + file + "\"";
    

    Thanks.



  • @qDebug
    Another way would be with QDesktopServices::openUrl(). This opens the file with the program that is defined as systems default. Should work with windows too (sorry i do not use windows :) )



  • @qDebug
    Agree with @the_ QDesktopServices::openUrl() is the way this should be done and I can confirm it works. In order to make sure video is opened in VLC you can set default program for video files as VLC player.



  • Thank you!

    QString file = filemodel->data(index, Qt::DisplayRole).toString();
    QDesktopServices::openUrl(QUrl::fromLocalFile(file));
    

    Works perfekt! :D


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.