How to locally disable qrc in QML: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=How to Locally Disable <span class="caps">QRC</span> in <span class="caps">QML</span>=
[[Category:Developing_with_Qt::Qt_Quick]]<br />[[Category:Snippets]]


By default, when you load any file in <span class="caps">QML</span> using <span class="caps">QRC</span> ([http://developer.qt.nokia.com/doc/qt-4.8/resources.html Qt Resource System] ''[developer.qt.nokia.com]''), 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 <span class="caps">QRC</span> without big changes to the source code.
= How to Locally Disable QRC in QML =


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, <span class="caps">QML</span> will not allow that – once you’ve started using resources, the declarative engine loads all files using <span class="caps">QRC</span> scheme.
By default, when you load any file in QML using QRC (&quot;Qt Resource System&amp;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.


I faced the same problem and found a pretty nice and easy solution: <span class="caps">QRC</span> can be locally disabled. It will not break anything – in fact, after turning the <span class="caps">QRC</span> scheme off, you can still load a file from resource system, and the <span class="caps">QML</span> engine will automatically reapply the scheme.
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.


Here’s what needs to be done (to make the example simple, I assume we need to disable <span class="caps">QRC</span> in a Loader element):
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 [http://developer.qt.nokia.com/doc/qt-4.8/qtbinding.html#embedding-c-objects-into-qml-components add as context properties to your <span class="caps">QML</span>] ''[developer.qt.nokia.com]''. In this case, I’ll use “void disableQrcSignal(QObject *object);as signal, and “void disableQrcSlot(QObject *object);as slot
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 &quot;add as context properties to your QML&amp;quot;:http://developer.qt.nokia.com/doc/qt-4.8/qtbinding.html#embedding-c-objects-into-qml-components. In this case, I'll use &quot;void disableQrcSignal(QObject *object);&quot; as signal, and &quot;void disableQrcSlot(QObject *object);&quot; as slot<br /># Before you change &quot;source&amp;quot; property of your Loader, send &quot;disableQrcSignal(loaderId)&quot; signal.
# 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):
Here's a snippet (I'm using a global property, fileToLoad, to store the path):


This ensures that <span class="caps">QRC</span> is disabled before you start loading the file. Of course, it can also be made in other ways.
<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:


And that’s it! Now you can use this signal in any place in your code to temporarily disable <span class="caps">QRC</span> loading.
<code><br />void MyClass::disableQrcSlot(QObject *object)<br />{<br /> // This assumes that QDeclarativeEngine is available<br /> QDeclarativeContext *context = engine()<s>&gt;contextForObject(object);<br /> // This line clears QRC scheme from object's context<br /> context</s>&gt;setBaseUrl(QUrl::fromLocalFile&amp;amp;#40;&quot;&quot;&amp;#41;);<br />}<br /></code>
 
===Categories:===
 
* [[:Category:Developing with Qt|Developing_with_Qt]]
** [[:Category:Developing with Qt::Qt Quick|Qt_Quick]]
* [[:Category:snippets|snippets]]

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

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&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
# Before you change "source&quot; 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>&gt;contextForObject(object);<br /> // This line clears QRC scheme from object's context<br /> context</s>&gt;setBaseUrl(QUrl::fromLocalFile&amp;amp;#40;&quot;&quot;&amp;#41;);<br />}<br />