<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- basictypes.qdoc --> <title>url QML Basic Type | Qt QML 5.9</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.9</td><td ><a href="qtqml-index.html">Qt QML</a></td><td ><a href="qtqml-qmlmodule.html">QML Types</a></td><td >url QML Basic Type</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#using-the-url-type">Using the url Type</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">url QML Basic Type</h1> <span class="subtitle"></span> <!-- $$$url-description --> <div class="descr"> <a name="details"></a> <p>a resource locator.</p> <p>The <code>url</code> type refers to a resource locator (like a file name, for example). It can be either absolute, e.g. "http://qt-project.org", or relative, e.g. "pics/logo.png". A relative URL is resolved relative to the URL of the containing component.</p> <p>For example, the following assigns a valid URL to the Image::source property, which is of type <code>url</code>:</p> <pre class="qml"> <span class="type">Image</span> { <span class="name">source</span>: <span class="string">"pics/logo.png"</span> } </pre> <p>When integrating with C++, note that any QUrl value <a href="qtqml-cppintegration-data.html">passed into QML from C++</a> is automatically converted into a <code>url</code> value, and vice-versa.</p> <a name="using-the-url-type"></a> <h2 id="using-the-url-type">Using the url Type</h2> <p>When a relative URL is written to a <code>url</code> type property, it is converted into a URL object, so <b>matching the URL value against the input string value will fail</b>. Instead, convert the string to a URL using Qt.resolvedUrl() for means of comparison, and use <code>toString()</code> to get the contents of the URL:</p> <pre class="qml"> <span class="type">Image</span> { <span class="name">source</span>: <span class="string">"pics/logo.png"</span> <span class="name">Component</span>.onCompleted: { <span class="comment">// This prints 'false'. Although "pics/logo.png" was the input string,</span> <span class="comment">// it's been converted from a string to a URL, so these two are not the same.</span> <span class="name">console</span>.<span class="name">log</span>(<span class="name">source</span> <span class="operator">==</span> <span class="string">"pics/logo.png"</span>) <span class="comment">// This prints 'true' as Qt.resovledUrl() converts the string into a</span> <span class="comment">// URL with the correctly resolved path</span> <span class="name">console</span>.<span class="name">log</span>(<span class="name">source</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">resolvedUrl</span>(<span class="string">"pics/logo.png"</span>)) <span class="comment">// This prints the absolute path, e.g. "file:///path/to/pics/logo.png"</span> <span class="name">console</span>.<span class="name">log</span>(<span class="name">source</span>.<span class="name">toString</span>()) } } </pre> <p><b>Note: </b>When referring to files stored with the Qt Resource System from within QML, you should use "qrc:///" instead of ":/" as QML requires URL paths. Relative URLs resolved from within that file will use the same protocol.</p><p>Additionally, URLs may contain encoded characters using the 'percent-encoding' scheme specified by <a href="http://tools.ietf.org/html/rfc3986">RFC 3986</a>. These characters will be preserved within properties of type <code>url</code>, to allow QML code to construct precise URL values. An exception to this rule is the preemptive decoding of directory-separator characters (<code>'/'</code>) - these characters are decoded to allow the URL to be correctly classified.</p> <p>For example, a local file containing a '#' character, which would normally be interpreted as the beginning of the URL 'fragment' element, can be accessed by encoding the characters of the file name:</p> <pre class="qml"> <span class="type">Image</span> { <span class="name">source</span>: <span class="name">encodeURIComponent</span>(<span class="string">"/tmp/test#1.png"</span>) } </pre> <p>This basic type is provided by the QML language.</p> </div> <p><b>See also </b><a href="qtqml-typesystem-basictypes.html">QML Basic Types</a>.</p> <!-- @@@url --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2017 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>