PDA

View Full Version : QSound and embedded resources



baray98
10th February 2009, 05:21
I was trying to load a wavefile for QSound from a qrc but of no luck. I tried loading the file from my drive then QSound made its sound. I am suspecting my QSound did not find the file in qrc. Is there any trick? I did all these in the snippet below



int main (int argc,char *argv[])
{

QApplication app(argc,argv);

QPixmap pixmap(":/Menu/tool.gif"); //QPixmap foound this file in my qrc
QSplashScreen splash(pixmap);
splash.show();
app.processEvents();

QSound::play(":/Sounds/voices/welcome.wav"); //I did not hear any on this one

MyWindow*mainWin = new MyWindow;
mainWin->setMinimumSize(1024,740);
mainWin->show();
splash.finish(mainWin);

return app.exec();
}


my qrc look like this



<qresource prefix="/Menu" >
<file>tool.gif</file>
</qresource>

<qresource prefix="/Sounds">
<file>voices/welcome.wav</file>
</qresource>


anything i missed?

baray98

jpn
10th February 2009, 14:37
anything i missed?
You missed the docs:


Note that QSound does not support resources. This might be fixed in a future Qt version.

yodreuc
28th February 2012, 07:26
Is there already a Qt version where QSound supports resources?

kuroi_neko
15th June 2012, 15:07
Nope.

However, you could use this trick:
1) define a class (ResourceFile) that will copy a resource to disk (in the hard-coded "resources" directory at executable file level in this example)

#ifndef RESOURCEFILE_H
#define RESOURCEFILE_H
#include <QFile>
#include <QString>

class ResourceFile : public QFile
{
public:
ResourceFile (const QString &respath, QObject * parent = 0);
};

#endif // RESOURCEFILE_H

#include "resourcefile.h"
#include <QFileInfo>
#include <QDir>

#define FILES_DIR "resources"

ResourceFile::ResourceFile (const QString & respath, QObject *parent)
: QFile (parent)
{
QFileInfo resource (respath);
QDir files = QDir::current();

if (!files.cd(FILES_DIR))
{
files.mkdir(FILES_DIR);
files.cd (FILES_DIR);
}

setFileName (files.filePath(resource.fileName()));
if (!exists())
{
QFile resfile(respath);
if(resfile.open(QIODevice::ReadOnly))
{
if (open(QIODevice::ReadWrite))
{
write(resfile.readAll());
close();
}
resfile.close();
}
}
}
2) define a class (ResourceSound) that will plug QSound to the disk copy

#ifndef RESOURCESOUND_H
#define RESOURCESOUND_H
#include <QSound>
#include "resourcefile.h"
class ResourceSound : private ResourceFile, public QSound
{
public:
ResourceSound (const QString & respath, QObject * parent = 0)
: ResourceFile (respath, parent)
, QSound (fileName(), parent) {}
};

#endif // RESOURCESOUND_H

You can then use ResourceSound instead of QSound and get the same result (or lack of, considering the shortcomings of QSound :)).

pip010
19th May 2017, 13:38
is that official, is QSound QSoundEffect not able to work with embedded resources?