Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Trying to play a resource file.



  • I'm trying to play a resource file to no avail.

    The code:

    QString ChimesWaveFile = "/home/nick/QT5Projects/PlayMusic/File-chimes.wav";

    .
    .
    .

        player->setMedia(QUrl::fromLocalFile(ChimesWaveFile));
        player->play();
    

    works just fine. But the code:

    QString ChimesWaveFile = ":/File-chimes.wav";

    .
    .
    .

        player->setMedia(QUrl::fromLocalFile(ChimesWaveFile));
        player->play();
    

    does not work in that nothing plays.

    What am I doing wrong?


  • Moderators

    A resource file is not local file. When you use QUrl::fromLocalFile(), it will prepend "file://" to the URL. So in the case of your resource path, the resulting URL will be wrong.

    Use default QUrl constructor (if using Qt 5) or QUrl::fromUserInput() (if using Qt 4).


Log in to reply