Grouping files into a container file / VFS / Access from QML
-
wrote on 3 Apr 2011, 07:33 last edited by
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:
@ Q_PROPERTY(QUrl image READ getImage NOTIFY imageChanged); @And in QML I'm doing:
@
Image {
source: backend.image
}
@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 -
wrote on 3 Apr 2011, 07:57 last edited by
A couple of options:
Use multiple resource files rather than one huge one if you are running out of memory at the compilation stage.
Take a look at "QAbstractFileEngineHandler":http://doc.qt.nokia.com/latest/qabstractfileenginehandler.html and "QAbstractFileEngine":http://doc.qt.nokia.com/latest/qabstractfileengine.html 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.
-
wrote on 3 Apr 2011, 08:16 last edited by
[quote author="ZapB" date="1301817472"]
Use multiple resource files rather than one huge one if you are running out of memory at the compilation stage.[/quote]
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.
[quote author="ZapB" date="1301817472"] # Take a look at "QAbstractFileEngineHandler":http://doc.qt.nokia.com/latest/qabstractfileenginehandler.html and "QAbstractFileEngine":http://doc.qt.nokia.com/latest/qabstractfileengine.html 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.[/quote]
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!
-
wrote on 18 Oct 2011, 19:29 last edited by
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)
@class MyContainer : public QFSFileEngine
{
public:
MyContainer( const QString & fn ) : QFSFileEngine( fn ){
setFileName( " CONTAINER FILE " );
}QString getInfos() { // I would like to have a folderstructure with some files: // //--container // | // -- header // | // -- infos.txt return ""; }
};
class MyContainerHandler : public QAbstractFileEngineHandler
{
public:
QAbstractFileEngine *create(const QString &fileName) const;
};QAbstractFileEngine *MyContainerHandler::create(const QString &fileName) const
{
return fileName.toLower().endsWith(".container") ? new MyContainer(fileName) : 0;
}@Thanks,
Attlia -
wrote on 18 Oct 2011, 19:43 last edited by
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.