How to locally disable qrc in QML: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:Developing_with_Qt::Qt_Quick]]<br />[[Category:Snippets]] | |||
= How to Locally Disable QRC in QML = | |||
By default, when you load any file in QML using QRC ("Qt Resource System&quot;:http://developer.qt.nokia.com/doc/qt-4.8/resources.html), all files down the hierarchy tree are loaded using resource system, too. This is nice and handy for most use cases, because it allows you to switch to QRC without big changes to the source code. | |||
But what if you are using resources, and suddenly need to load a non-qrc file? For example, in an app you are developing, a certain module, or game's save file, can be provided by user at runtime, using relative path. Currently, QML will not allow that - once you've started using resources, the declarative engine loads all files using QRC scheme. | |||
I faced the same problem and found a pretty nice and easy solution: QRC can be locally disabled. It will not break anything - in fact, after turning the QRC scheme off, you can still load a file from resource system, and the QML engine will automatically reapply the scheme. | |||
# In C++, define a signal to be emitted before the new file is to be loaded, and a slot to receive the signal. Connect them together, and | Here's what needs to be done (to make the example simple, I assume we need to disable QRC in a Loader element):<br /># In C++, define a signal to be emitted before the new file is to be loaded, and a slot to receive the signal. Connect them together, and "add as context properties to your QML&quot;:http://developer.qt.nokia.com/doc/qt-4.8/qtbinding.html#embedding-c-objects-into-qml-components. In this case, I'll use "void disableQrcSignal(QObject *object);" as signal, and "void disableQrcSlot(QObject *object);" as slot<br /># Before you change "source&quot; property of your Loader, send "disableQrcSignal(loaderId)" signal. | ||
# Before you change | |||
Here's a snippet (I'm using a global property, fileToLoad, to store the path): | |||
<code><br />Loader {<br /> id: loader<br /> source: {<br /> disableQrcUse(loader);<br /> return fileToLoad; // A relative path or QRC path!<br /> }<br />}<br /></code><br />This ensures that QRC is disabled before you start loading the file. Of course, it can also be made in other ways. | |||
In your disableQrcSlot() body, use this code: | In your disableQrcSlot() body, use this code: | ||
<code><br />void MyClass::disableQrcSlot(QObject *object)<br />{<br /> // This assumes that QDeclarativeEngine is available<br /> QDeclarativeContext *context = engine()<s>>contextForObject(object);<br /> // This line clears QRC scheme from object's context<br /> context</s>>setBaseUrl(QUrl::fromLocalFile&amp;#40;""&#41;);<br />}<br /></code> | |||
** | |||
Revision as of 10:43, 24 February 2015
How to Locally Disable QRC in QML
By default, when you load any file in QML using QRC ("Qt Resource System":http://developer.qt.nokia.com/doc/qt-4.8/resources.html), all files down the hierarchy tree are loaded using resource system, too. This is nice and handy for most use cases, because it allows you to switch to QRC without big changes to the source code.
But what if you are using resources, and suddenly need to load a non-qrc file? For example, in an app you are developing, a certain module, or game's save file, can be provided by user at runtime, using relative path. Currently, QML will not allow that - once you've started using resources, the declarative engine loads all files using QRC scheme.
I faced the same problem and found a pretty nice and easy solution: QRC can be locally disabled. It will not break anything - in fact, after turning the QRC scheme off, you can still load a file from resource system, and the QML engine will automatically reapply the scheme.
Here's what needs to be done (to make the example simple, I assume we need to disable QRC in a Loader element):
# In C++, define a signal to be emitted before the new file is to be loaded, and a slot to receive the signal. Connect them together, and "add as context properties to your QML":http://developer.qt.nokia.com/doc/qt-4.8/qtbinding.html#embedding-c-objects-into-qml-components. In this case, I'll use "void disableQrcSignal(QObject *object);" as signal, and "void disableQrcSlot(QObject *object);" as slot
# Before you change "source" property of your Loader, send "disableQrcSignal(loaderId)" signal.
Here's a snippet (I'm using a global property, fileToLoad, to store the path):
<br />Loader {<br /> id: loader<br /> source: {<br /> disableQrcUse(loader);<br /> return fileToLoad; // A relative path or QRC path!<br /> }<br />}<br />
This ensures that QRC is disabled before you start loading the file. Of course, it can also be made in other ways.
In your disableQrcSlot() body, use this code:
<br />void MyClass::disableQrcSlot(QObject *object)<br />{<br /> // This assumes that QDeclarativeEngine is available<br /> QDeclarativeContext *context = engine()<s>>contextForObject(object);<br /> // This line clears QRC scheme from object's context<br /> context</s>>setBaseUrl(QUrl::fromLocalFile&amp;#40;""&#41;);<br />}<br />