Results 1 to 5 of 5

Thread: QSound and embedded resources

  1. #1
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QSound and embedded resources

    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
    Qt Code:
    1. int main (int argc,char *argv[])
    2. {
    3.  
    4. QApplication app(argc,argv);
    5.  
    6. QPixmap pixmap(":/Menu/tool.gif"); //QPixmap foound this file in my qrc
    7. QSplashScreen splash(pixmap);
    8. splash.show();
    9. app.processEvents();
    10.  
    11. QSound::play(":/Sounds/voices/welcome.wav"); //I did not hear any on this one
    12.  
    13. MyWindow*mainWin = new MyWindow;
    14. mainWin->setMinimumSize(1024,740);
    15. mainWin->show();
    16. splash.finish(mainWin);
    17.  
    18. return app.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 
    my qrc look like this
    Qt Code:
    1. <qresource prefix="/Menu" >
    2. <file>tool.gif</file>
    3. </qresource>
    4.  
    5. <qresource prefix="/Sounds">
    6. <file>voices/welcome.wav</file>
    7. </qresource>
    To copy to clipboard, switch view to plain text mode 

    anything i missed?

    baray98

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSound and embedded resources

    Quote Originally Posted by baray98 View Post
    anything i missed?
    You missed the docs:
    Note that QSound does not support resources. This might be fixed in a future Qt version.
    J-P Nurmi

  3. #3
    Join Date
    Nov 2011
    Posts
    5
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: QSound and embedded resources

    Is there already a Qt version where QSound supports resources?

  4. #4
    Join Date
    Jun 2012
    Location
    Paris, France
    Posts
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSound and embedded resources

    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)
    Qt Code:
    1. #ifndef RESOURCEFILE_H
    2. #define RESOURCEFILE_H
    3. #include <QFile>
    4. #include <QString>
    5.  
    6. class ResourceFile : public QFile
    7. {
    8. public:
    9. ResourceFile (const QString &respath, QObject * parent = 0);
    10. };
    11.  
    12. #endif // RESOURCEFILE_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "resourcefile.h"
    2. #include <QFileInfo>
    3. #include <QDir>
    4.  
    5. #define FILES_DIR "resources"
    6.  
    7. ResourceFile::ResourceFile (const QString & respath, QObject *parent)
    8. : QFile (parent)
    9. {
    10. QFileInfo resource (respath);
    11. QDir files = QDir::current();
    12.  
    13. if (!files.cd(FILES_DIR))
    14. {
    15. files.mkdir(FILES_DIR);
    16. files.cd (FILES_DIR);
    17. }
    18.  
    19. setFileName (files.filePath(resource.fileName()));
    20. if (!exists())
    21. {
    22. QFile resfile(respath);
    23. if(resfile.open(QIODevice::ReadOnly))
    24. {
    25. if (open(QIODevice::ReadWrite))
    26. {
    27. write(resfile.readAll());
    28. close();
    29. }
    30. resfile.close();
    31. }
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 
    2) define a class (ResourceSound) that will plug QSound to the disk copy
    Qt Code:
    1. #ifndef RESOURCESOUND_H
    2. #define RESOURCESOUND_H
    3. #include <QSound>
    4. #include "resourcefile.h"
    5. class ResourceSound : private ResourceFile, public QSound
    6. {
    7. public:
    8. ResourceSound (const QString & respath, QObject * parent = 0)
    9. : ResourceFile (respath, parent)
    10. , QSound (fileName(), parent) {}
    11. };
    12.  
    13. #endif // RESOURCESOUND_H
    To copy to clipboard, switch view to plain text mode 

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

  5. #5
    Join Date
    Feb 2016
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSound and embedded resources

    is that official, is QSound QSoundEffect not able to work with embedded resources?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.