Results 1 to 5 of 5

Thread: QSound and embedded resources

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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?

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

    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 ).

  3. #3
    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
  •  
Qt is a trademark of The Qt Company.