April 3, 2011

conny conny
Lab Rat
85 posts

Grouping files into a container file / VFS / Access from QML

 

Hi,

I have hundreds of files that I want to deploy with my application. For several reasons I’d like to put all of them into one file and then access them using QUrl from QML. Basically I would like to use the Qt resource system, but I can’t. It’s too much data and my machine cannot handle the compilation.

So I thought about putting all those files into a ZIP-file (or similar). Now, I know how to access that data from C++, but I’m unsure what interface I have to implement to make the content available to QML. Is QIODevice the way to go? If yes, how can I get it to work with QUrl?

Currently my C++ backend has properties like:

  1.  Q_PROPERTY(QUrl image READ getImage NOTIFY imageChanged);

And in QML I’m doing:

  1. Image {
  2.     source: backend.image
  3. }

I hope to be able to keep all that code and transparently access the ZIP file instead of the resource file. If that does not work it would also be ok for me to change the URLs to something like zip:///somedir/image.jpg

It would be great if you could give me some pointers in the right direction. Especially the QUrl/QML integration.

Thanks!
Conny

4 replies

April 3, 2011

ZapB ZapB
Ant Farmer
1424 posts

A couple of options:

  1. Use multiple resource files rather than one huge one if you are running out of memory at the compilation stage.
  1. Take a look at QAbstractFileEngineHandler [doc.qt.nokia.com] and QAbstractFileEngine [doc.qt.nokia.com] classes to implement your own virtual file system. You could wrap up some existing format (zip, tarball, mpq) or invent your own. This approach will allow you to use your own URL prefix as you mentioned above.
 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

April 3, 2011

conny conny
Lab Rat
85 posts

ZapB wrote:
  1. Use multiple resource files rather than one huge one if you are running out of memory at the compilation stage.

I have around 100MB and from my experience it can take ages to compile that even if you have enough RAM, so I think option 2 is better suited.

ZapB wrote:
# Take a look at QAbstractFileEngineHandler [doc.qt.nokia.com] and QAbstractFileEngine [doc.qt.nokia.com] classes to implement your own virtual file system. You could wrap up some existing format (zip, tarball, mpq) or invent your own. This approach will allow you to use your own URL prefix as you mentioned above.

That looks like exactly what I need. I’ll start trying this with zip since I think it’s better suited for random-access than tar.

Thanks a lot!

October 18, 2011

attila.katona attila.katona
Lab Rat
1 posts

Hi,

I would also need to group some files and directories (images, xml,…) into one container file. I just can not find out, how should I do it with QAbstractFileEngineHandler and QAbstractFileEngine. Could you give me a small example? (Here is my example)

  1. class MyContainer : public QFSFileEngine
  2. {
  3.     public:
  4.         MyContainer( const QString & fn ) : QFSFileEngine( fn ){
  5.             setFileName( " CONTAINER FILE " );
  6.         }
  7.  
  8.         QString getInfos()
  9.         {
  10.             // I would like to have a folderstructure with some files:
  11.             //
  12.             //--container
  13.             //    |
  14.             //    -- header
  15.             //        |
  16.             //        -- infos.txt
  17.  
  18.             return "";
  19.         }
  20. };
  21.  
  22. class MyContainerHandler : public QAbstractFileEngineHandler
  23. {
  24.     public:
  25.         QAbstractFileEngine *create(const QString &fileName) const;
  26. };
  27.  
  28. QAbstractFileEngine *MyContainerHandler::create(const QString &fileName) const
  29. {    
  30.     return fileName.toLower().endsWith(".container") ? new MyContainer(fileName) : 0;
  31. }

Thanks,
Attlia

October 18, 2011

moellney moellney
Lab Rat
16 posts

Note that in
http://labs.qt.nokia.com/2011/05/12/qt-modules-maturity-level-the-list/

you can read:

Abstract file engines
State: Deprecated
Reasoning: flawed design, this is the wrong level to provide a virtual filesystem, so we don’t recommend taking this over. In Qt 5, this functionality will be Removed.

 
  ‹‹ [SOLVED] Creating SIS package problem      Compiling Qt Embedded for Mips ››

You must log in to post a reply. Not a member yet? Register here!