qtwebengine-webengine-recipebrowser-example.html 40.2 KB
Newer Older
xuebingbing's avatar
xuebingbing committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- recipebrowser.qdoc -->
  <title>WebEngine Recipe Browser | Qt WebEngine 5.11</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 ><a href="../qtdoc/index.html">Qt 5.11</a></td><td ><a href="qtwebengine-index.html">Qt WebEngine</a></td><td ><a href="webengine-examples.html">Qt WebEngine Examples</a></td><td >WebEngine Recipe Browser</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.11.2 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="#running-the-example">Running the Example</a></li>
<li class="level1"><a href="#c-code">C++ Code</a></li>
<li class="level1"><a href="#qml-code">QML Code</a></li>
<li class="level1"><a href="#files-and-attributions">Files and Attributions</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">WebEngine Recipe Browser</h1>
<span class="subtitle"></span>
<!-- $$$webengine/recipebrowser-brief -->
<p>A small hybrid application based on the <a href="qml-qtwebengine-webengineview.html">WebEngineView</a> QML type and Qt Quick Controls 2.</p>
<!-- @@@webengine/recipebrowser -->
<!-- $$$webengine/recipebrowser-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/recipebrowser-demo.jpg" alt="" /></p><p><i>Recipe Browser</i> demonstrates how to use the <a href="qml-qtwebengine-webengineview.html">WebEngineView</a> item, <a href="../qtquick/qtquick-index.html">Qt Quick</a> items, and <a href="../qtquickcontrols2/qtquickcontrols2-index.html">Qt Quick Controls 2</a> items to develop a small hybrid web browser application. A <a href="../qtquick/qml-qtquick-listview.html">ListView</a>-based item is used to display a list of recipe names. Clicking on a name causes the web view to load the respective recipe page. The overall appearance of the application is provided by the <a href="../qtquickcontrols2/qtquickcontrols2-index.html">Qt Quick Controls 2</a> items, which have their active style set to the <a href="../qtquickcontrols2/qtquickcontrols2-styles.html#material-style">Material</a> style. The web content is a mix of HTML and Markdown source compiled to HTML, along with CSS and JavaScript.</p>
<a name="running-the-example"></a>
<h2 id="running-the-example">Running the Example</h2>
<p>To run the example from <a href="http://doc.qt.io/qtcreator/index.html">Qt Creator</a>, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit <a href="http://doc.qt.io/qtcreator/creator-build-example-application.html">Building and Running an Example</a>.</p>
<a name="c-code"></a>
<h2 id="c-code">C++ Code</h2>
<p>In <code>main.cpp</code>, we use the <a href="../qtgui/qguiapplication.html">QGuiApplication</a> and <a href="../qtqml/qqmlapplicationengine.html">QQmlApplicationEngine</a> classes to set up and load the main QML file. We call <a href="qtwebengine.html#initialize">QtWebEngine::initialize</a> so we can use <a href="qml-qtwebengine-webengineview.html">WebEngineView</a> in our QML code. We enable high DPI screen support by setting the <a href="../qtcore/qt.html#ApplicationAttribute-enum">Qt::AA_EnableHighDpiScaling</a> attribute. We set the default Qt Quick Controls 2 style to the Material style, so we do not have to specify it for each new item we add. Finally, we use a C++ define to check whether the application is compiled for an embedded platform. The value will be used in the main QML code to determine the window size.</p>
<pre class="cpp">

  <span class="preprocessor">#include &lt;QGuiApplication&gt;</span>
  <span class="preprocessor">#include &lt;QQmlApplicationEngine&gt;</span>
  <span class="preprocessor">#include &lt;QQmlContext&gt;</span>
  <span class="preprocessor">#include &lt;QQuickStyle&gt;</span>
  <span class="preprocessor">#include &lt;qtwebengineglobal.h&gt;</span>

  <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
  {
      <span class="type"><a href="../qtcore/qcoreapplication.html">QCoreApplication</a></span><span class="operator">::</span>setAttribute(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AA_EnableHighDpiScaling);
      <span class="type"><a href="../qtgui/qguiapplication.html">QGuiApplication</a></span> app(argc<span class="operator">,</span> argv);
      <span class="type"><a href="qtwebengine-module.html">QtWebEngine</a></span><span class="operator">::</span>initialize();

      <span class="type"><a href="../qtquickcontrols2/qquickstyle.html">QQuickStyle</a></span><span class="operator">::</span>setStyle(<span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">&quot;Material&quot;</span>));

      <span class="type"><a href="../qtqml/qqmlapplicationengine.html">QQmlApplicationEngine</a></span> engine;

      bool isEmbedded <span class="operator">=</span> <span class="keyword">false</span>;
  <span class="preprocessor">#ifdef QTWEBENGINE_RECIPE_BROWSER_EMBEDDED</span>
      isEmbedded <span class="operator">=</span> <span class="keyword">true</span>;
  <span class="preprocessor">#endif</span>
      engine<span class="operator">.</span>rootContext()<span class="operator">-</span><span class="operator">&gt;</span>setContextProperty(<span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">&quot;isEmbedded&quot;</span>)<span class="operator">,</span> isEmbedded);

      engine<span class="operator">.</span>load(<span class="type"><a href="../qtcore/qurl.html">QUrl</a></span>(<span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">&quot;qrc:/qml/main.qml&quot;</span>)));

      <span class="keyword">return</span> app<span class="operator">.</span>exec();
  }

</pre>
<a name="qml-code"></a>
<h2 id="qml-code">QML Code</h2>
<p>In the <code>main.qml</code> file, we first create a top-level window and set a title for it. We also set up the size of the window depending on its primary orientation as well as the platform, so that the application is usable on both desktop and embedded platforms. On desktop, the size is constrained by a minimum of 320x480 pixels up to the maximum size that the screen supports. The default window size is 1024 pixels wide and 768 pixels high in landscape orientation. On embedded devices, the window will occupy the whole screen.</p>
<pre class="cpp">

  <span class="type"><a href="../qtquickcontrols/qml-qtquick-controls-applicationwindow.html">ApplicationWindow</a></span> {
      <span class="name">id</span>: <span class="name">appWindow</span>
      <span class="name">title</span>: <span class="name">qsTr</span>(<span class="string">&quot;Recipe Browser&quot;</span>)
      <span class="name">visible</span>: <span class="number">true</span>

      property <span class="type">int</span> <span class="name">shorterDesktop</span>: <span class="number">768</span>
      property <span class="type">int</span> <span class="name">longerDesktop</span>: <span class="number">1024</span>
      property <span class="type">int</span> <span class="name">shorterMin</span>: <span class="number">360</span>
      property <span class="type">int</span> <span class="name">longerMin</span>: <span class="number">480</span>
      property <span class="type">bool</span> <span class="name">isPortrait</span>: <span class="name">Screen</span>.<span class="name">primaryOrientation</span> <span class="operator">===</span> <span class="name">Qt</span>.<span class="name">PortraitOrientation</span>
      <span class="name">width</span>: {
          <span class="keyword">if</span> (<span class="name">isEmbedded</span>)
              <span class="keyword">return</span> <span class="name">Screen</span>.<span class="name">width</span>
          var <span class="name">potentialWidth</span> = <span class="name">shorterDesktop</span>
          <span class="keyword">if</span> (!<span class="name">isPortrait</span>)
              <span class="name">potentialWidth</span> <span class="operator">=</span> <span class="name">longerDesktop</span>
          <span class="keyword">return</span> <span class="name">potentialWidth</span> <span class="operator">&gt;</span> <span class="name">Screen</span>.<span class="name">width</span> ? <span class="name">Screen</span>.<span class="name">width</span> : <span class="name">potentialWidth</span>
      }
      <span class="name">height</span>: {
          <span class="keyword">if</span> (<span class="name">isEmbedded</span>)
              <span class="keyword">return</span> <span class="name">Screen</span>.<span class="name">height</span>
          var <span class="name">potentialHeight</span> = <span class="name">longerDesktop</span>
          <span class="keyword">if</span> (!<span class="name">isPortrait</span>)
              <span class="name">potentialHeight</span> <span class="operator">=</span> <span class="name">shorterDesktop</span>
          <span class="keyword">return</span> <span class="name">potentialHeight</span> <span class="operator">&gt;</span> <span class="name">Screen</span>.<span class="name">height</span> ? <span class="name">Screen</span>.<span class="name">height</span> : <span class="name">potentialHeight</span>
      }
      <span class="name">minimumWidth</span>: <span class="name">isPortrait</span> ? <span class="name">shorterMin</span> : <span class="name">longerMin</span>
      <span class="name">minimumHeight</span>: <span class="name">isPortrait</span> ? <span class="name">longerMin</span> : <span class="name">shorterMin</span>

</pre>
<p>Next, we add a <a href="../qtquick/qml-qtquick-layouts-rowlayout.html">RowLayout</a> item so we can divide the window into two parts: one being a custom <code>RecipeList</code> item containing the recipe titles, and the other being the <a href="qml-qtwebengine-webengineview.html">WebEngineView</a>, which shows the recipe details. The spacing is set to zero so the items are positioned directly next to each other.</p>
<pre class="cpp">

      <span class="type"><a href="../qtquick/qml-qtquick-layouts-rowlayout.html">RowLayout</a></span> {
          <span class="name">id</span>: <span class="name">container</span>
          <span class="name">anchors</span>.fill: <span class="name">parent</span>
          <span class="name">spacing</span>: <span class="number">0</span>

          <span class="type">RecipeList</span> {
                  ...
              }
          }

          <span class="type"><a href="qml-qtwebengine-webengineview.html">WebEngineView</a></span> {
                  ...
          }
      }

</pre>
<p>The <code>RecipeList</code> item has a few <a href="../qtquick/qml-qtquick-layouts-layout.html">attached Layout properties</a>, in order to scale the item to a maximum of one third of the layout width. We give the item focus, so that the keyboard can be used to navigate the recipes, in addition to using mouse and touch. We also add a handler for the custom <code>recipeSelected</code> signal, to tell the <a href="qml-qtwebengine-webengineview.html">WebEngineView</a> to load the URL of the selected recipe.</p>
<pre class="cpp">

          <span class="type">RecipeList</span> {
              <span class="name">id</span>: <span class="name">recipeList</span>
              <span class="name">Layout</span>.minimumWidth: <span class="number">124</span>
              <span class="name">Layout</span>.preferredWidth: <span class="name">parent</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">3</span>
              <span class="name">Layout</span>.maximumWidth: <span class="number">300</span>
              <span class="name">Layout</span>.fillWidth: <span class="number">true</span>
              <span class="name">Layout</span>.fillHeight: <span class="number">true</span>
              <span class="name">focus</span>: <span class="number">true</span>
              <span class="name">KeyNavigation</span>.tab: <span class="name">webView</span>
              <span class="name">onRecipeSelected</span>: <span class="keyword">function</span>(<span class="name">url</span>) {
                  <span class="name">webView</span>.<span class="name">showRecipe</span>(<span class="name">url</span>)
              }

</pre>
<p>The <a href="qml-qtwebengine-webengineview.html">WebEngineView</a> has similar layout properties, to make it occupy two thirds of the layout width.</p>
<pre class="cpp">

          <span class="type"><a href="qml-qtwebengine-webengineview.html">WebEngineView</a></span> {
              <span class="name">id</span>: <span class="name">webView</span>
              <span class="name">Layout</span>.preferredWidth: <span class="number">2</span> <span class="operator">*</span> <span class="name">parent</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">3</span>
              <span class="name">Layout</span>.fillWidth: <span class="number">true</span>
              <span class="name">Layout</span>.fillHeight: <span class="number">true</span>
              <span class="name">KeyNavigation</span>.tab: <span class="name">recipeList</span>
              <span class="name">KeyNavigation</span>.priority: <span class="name">KeyNavigation</span>.<span class="name">BeforeItem</span>

</pre>
<p>We then disable the <a href="qml-qtwebengine-webenginesettings.html#focusOnNavigationEnabled-prop">focusOnNavigationEnabled</a> setting to make sure that the <a href="qml-qtwebengine-webengineview.html">WebEngineView</a> does not steal focus from the <code>RecipeList</code> item every time its URL is changed. This allows the user to continue navigating through the recipes using the keyboard. We also disable the default context menu by accepting the <a href="qml-qtwebengine-contextmenurequest.html">ContextMenuRequest</a>.</p>
<pre class="cpp">

              <span class="name">settings</span>.focusOnNavigationEnabled: <span class="number">false</span>

              <span class="name">onContextMenuRequested</span>: <span class="keyword">function</span>(<span class="name">request</span>) {
                  <span class="name">request</span>.<span class="name">accepted</span> <span class="operator">=</span> <span class="number">true</span>
              }

</pre>
<p>When the application starts, instead of directly showing the <a href="qml-qtwebengine-webengineview.html">WebEngineView</a>, we show a placeholder <a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a> with a <a href="../qtquickcontrols/qml-qtquick-controls-busyindicator.html">BusyIndicator</a> to provide a nicer user experience while the application is loading. Once the first page in the view is loaded, we start a <a href="../qtqml/qml-qtqml-timer.html">Timer</a> that will hide the placeholder and show the actual page. The delay provides more time for the recipe images to load, so that when the view is shown, the page is completely rendered. The timer also shows a help <a href="../qtquickcontrols2/qml-qtquick-controls2-tooltip.html">ToolTip</a> that informs the user on how to navigate the recipes.</p>
<pre class="cpp">

              property <span class="type">bool</span> <span class="name">firstLoadComplete</span>: <span class="number">false</span>
              <span class="name">onLoadingChanged</span>: <span class="keyword">function</span>(<span class="name">loadRequest</span>) {
                  <span class="keyword">if</span> (<span class="name">loadRequest</span>.<span class="name">status</span> <span class="operator">===</span> <span class="name">WebEngineView</span>.<span class="name">LoadSucceededStatus</span>
                      <span class="operator">&amp;&amp;</span> !<span class="name">firstLoadComplete</span>) {
                      <span class="comment">// Debounce the showing of the web content, so images are more likely</span>
                      <span class="comment">// to have loaded completely.</span>
                      <span class="name">showTimer</span>.<span class="name">start</span>()
                  }
              }

              <span class="type"><a href="../qtqml/qml-qtqml-timer.html">Timer</a></span> {
                  <span class="name">id</span>: <span class="name">showTimer</span>
                  <span class="name">interval</span>: <span class="number">500</span>
                  <span class="name">repeat</span>: <span class="number">false</span>
                  <span class="name">onTriggered</span>: {
                      <span class="name">webView</span>.<span class="name">show</span>(<span class="number">true</span>)
                      <span class="name">webView</span>.<span class="name">firstLoadComplete</span> <span class="operator">=</span> <span class="number">true</span>
                      <span class="name">recipeList</span>.<span class="name">showHelp</span>()
                  }
              }

              <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
                  <span class="name">id</span>: <span class="name">webViewPlaceholder</span>
                  <span class="name">anchors</span>.fill: <span class="name">parent</span>
                  <span class="name">z</span>: <span class="number">1</span>
                  <span class="name">color</span>: <span class="string">&quot;white&quot;</span>

                  <span class="type"><a href="../qtquickcontrols/qml-qtquick-controls-busyindicator.html">BusyIndicator</a></span> {
                      <span class="name">id</span>: <span class="name">busy</span>
                      <span class="name">anchors</span>.centerIn: <span class="name">parent</span>
                  }
              }

              <span class="keyword">function</span> <span class="name">showRecipe</span>(<span class="name">url</span>) {
                  <span class="name">webView</span>.<span class="name">url</span> <span class="operator">=</span> <span class="name">url</span>
              }

              <span class="keyword">function</span> <span class="name">show</span>(<span class="name">show</span>) {
                  <span class="keyword">if</span> (<span class="name">show</span> <span class="operator">===</span> <span class="number">true</span>) {
                      <span class="name">busy</span>.<span class="name">running</span> <span class="operator">=</span> <span class="number">false</span>
                      <span class="name">webViewPlaceholder</span>.<span class="name">visible</span> <span class="operator">=</span> <span class="number">false</span>
                  } <span class="keyword">else</span> {
                      <span class="name">webViewPlaceholder</span>.<span class="name">visible</span> <span class="operator">=</span> <span class="number">true</span>
                      <span class="name">busy</span>.<span class="name">running</span> <span class="operator">=</span> <span class="number">true</span>
                  }
              }
          }

</pre>
<p>Let's see what the <code>RecipeList</code> item looks like from the inside. The root item is a <a href="../qtquick/qml-qtquick-focusscope.html">FocusScope</a> to allow transferring focus to the child <a href="../qtquick/qml-qtquick-listview.html">ListView</a> whenever the root item receives focus. We also declare a custom <code>recipeSelected</code> signal, which will be emitted when the current item of the <a href="../qtquick/qml-qtquick-listview.html">ListView</a> changes.</p>
<pre class="cpp">

  <span class="type"><a href="../qtquick/qml-qtquick-focusscope.html">FocusScope</a></span> {
      <span class="name">id</span>: <span class="name">root</span>
      signal <span class="type">recipeSelected</span>(url url)

</pre>
<p>A <a href="../qtquick/qml-qtquick-layouts-columnlayout.html">ColumnLayout</a> holds a header <a href="../qtquickcontrols/qml-qtquick-controls-label.html">Label</a> above the <a href="../qtquick/qml-qtquick-listview.html">ListView</a>, and the <a href="../qtquick/qml-qtquick-listview.html">ListView</a> itself. Again, we set the spacing to zero and make sure the layout occupies the whole space of the parent item.</p>
<pre class="cpp">

      <span class="type"><a href="../qtquick/qml-qtquick-layouts-columnlayout.html">ColumnLayout</a></span> {
          <span class="name">spacing</span>: <span class="number">0</span>
          <span class="name">anchors</span>.fill: <span class="name">parent</span>

</pre>
<p>Inside the layout there is a styled <a href="../qtquickcontrols/qml-qtquick-controls-toolbar.html">ToolBar</a> item, with a <a href="../qtquickcontrols/qml-qtquick-controls-label.html">Label</a> inside of it serving as the <a href="../qtquick/qml-qtquick-listview.html">ListView</a> header.</p>
<pre class="cpp">

          <span class="type"><a href="../qtquickcontrols/qml-qtquick-controls-toolbar.html">ToolBar</a></span> {
              <span class="name">id</span>: <span class="name">headerBackground</span>
              <span class="name">Layout</span>.fillWidth: <span class="number">true</span>
              <span class="name">implicitHeight</span>: <span class="name">headerText</span>.<span class="name">height</span> <span class="operator">+</span> <span class="number">20</span>

              <span class="type"><a href="../qtquickcontrols/qml-qtquick-controls-label.html">Label</a></span> {
                  <span class="name">id</span>: <span class="name">headerText</span>
                  <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span>
                  <span class="name">text</span>: <span class="name">qsTr</span>(<span class="string">&quot;Favorite recipes&quot;</span>)
                  <span class="name">padding</span>: <span class="number">10</span>
                  <span class="name">anchors</span>.centerIn: <span class="name">parent</span>
              }
          }

</pre>
<p>The second item inside the layout is a <a href="../qtquick/qml-qtquick-listview.html">ListView</a>, whose contents will fill the remaining space in the layout. We set <a href="../qtquick/qml-qtquick-item.html#clip-prop">clip</a> to true, so that the delegates that are scrolled up are not seen under the <a href="../qtquickcontrols/qml-qtquick-controls-toolbar.html">ToolBar</a> item. We set <a href="../qtquick/qml-qtquick-item.html#focus-prop">focus</a> to true, so the <a href="../qtquick/qml-qtquick-listview.html">ListView</a> gains focus when the <a href="../qtquick/qml-qtquick-focusscope.html">FocusScope</a> does. We add a vertical scroll bar, so the user can scroll through the recipes if the window size is small. We also specify the recipe model to be used by the <a href="../qtquick/qml-qtquick-listview.html">ListView</a> as described later in this topic.</p>
<pre class="cpp">

          <span class="type"><a href="../qtquick/qml-qtquick-listview.html">ListView</a></span> {
              <span class="name">id</span>: <span class="name">listView</span>
              <span class="name">Layout</span>.fillWidth: <span class="number">true</span>
              <span class="name">Layout</span>.fillHeight: <span class="number">true</span>
              <span class="name">keyNavigationWraps</span>: <span class="number">true</span>
              <span class="name">clip</span>: <span class="number">true</span>
              <span class="name">focus</span>: <span class="number">true</span>
              <span class="name">ScrollBar</span>.vertical: <span class="name">ScrollBar</span> { }

              <span class="name">model</span>: <span class="name">recipeModel</span>

</pre>
<p>We have an <a href="../qtquickcontrols2/qml-qtquick-controls2-itemdelegate.html">ItemDelegate</a> set as the <a href="../qtquick/qml-qtquick-listview.html">ListView</a> delegate, which displays the recipe title. The contentItem is a <a href="../qtquick/qml-qtquick-text.html">Text</a> item, customized with a few properties to adjust the visual appearance and position of the text. We create a binding to the current delegate's model URL, so we can access the respective URL outside the delegate itself. We set the <a href="../qtquickcontrols2/qml-qtquick-controls2-itemdelegate.html#highlighted-prop">highlighted</a> property to <code>true</code> whenever the item is the current one in the <a href="../qtquick/qml-qtquick-listview.html">ListView</a> to provide visual feedback. And we set the focus on the <a href="../qtquick/qml-qtquick-listview.html">ListView</a> whenever a delegate is clicked, so that keyboard navigation works in case the focus was previously in the <a href="qml-qtwebengine-webengineview.html">WebEngineView</a>.</p>
<pre class="cpp">

              <span class="name">delegate</span>: <span class="name">ItemDelegate</span> {
                  <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span>
                  <span class="name">text</span>: <span class="name">model</span>.<span class="name">name</span>
                  <span class="name">contentItem</span>: <span class="name">Text</span> {
                      <span class="name">text</span>: <span class="name">parent</span>.<span class="name">text</span>
                      <span class="name">font</span>: <span class="name">parent</span>.<span class="name">font</span>
                      <span class="name">color</span>: <span class="name">parent</span>.<span class="name">enabled</span> ? <span class="name">parent</span>.<span class="name">Material</span>.<span class="name">primaryTextColor</span>
                                            : <span class="name">parent</span>.<span class="name">Material</span>.<span class="name">hintTextColor</span>
                      <span class="name">elide</span>: <span class="name">Text</span>.<span class="name">ElideRight</span>
                      <span class="name">horizontalAlignment</span>: <span class="name">Text</span>.<span class="name">AlignLeft</span>
                      <span class="name">verticalAlignment</span>: <span class="name">Text</span>.<span class="name">AlignVCenter</span>
                      <span class="name">wrapMode</span>: <span class="name">Text</span>.<span class="name">Wrap</span>
                  }

                  property <span class="type">url</span> <span class="name">url</span>: <span class="name">model</span>.<span class="name">url</span>
                  <span class="name">highlighted</span>: <span class="name">ListView</span>.<span class="name">isCurrentItem</span>

                  <span class="name">onClicked</span>: {
                      <span class="name">listView</span>.<span class="name">forceActiveFocus</span>()
                      <span class="name">listView</span>.<span class="name">currentIndex</span> <span class="operator">=</span> <span class="name">model</span>.<span class="name">index</span>
                  }
              }

</pre>
<p>A handler is defined for the <code>currentItemChanged</code> signal to emit our own <code>recipeSelected</code> signal with the URL that the <a href="qml-qtwebengine-webengineview.html">WebEngineView</a> should load.</p>
<pre class="cpp">

              <span class="name">onCurrentItemChanged</span>: {
                  <span class="name">root</span>.<span class="name">recipeSelected</span>(<span class="name">currentItem</span>.<span class="name">url</span>)
              }

</pre>
<p>We use a <a href="../qtqml/qml-qtqml-models-listmodel.html">ListModel</a> with seven <a href="../qtqml/qml-qtqml-models-listelement.html">ListElement</a>s, each of which contains a recipe title and the URL to an HTML page contained in a resource file. The model is used to populate the <a href="../qtquick/qml-qtquick-listview.html">ListView</a> with the recipes and to show the recipe details in the <a href="qml-qtwebengine-webengineview.html">WebEngineView</a>.</p>
<pre class="cpp">

              <span class="type"><a href="../qtqml/qml-qtqml-models-listmodel.html">ListModel</a></span> {
                  <span class="name">id</span>: <span class="name">recipeModel</span>

                  <span class="type"><a href="../qtqml/qml-qtqml-models-listelement.html">ListElement</a></span> {
                      <span class="name">name</span>: <span class="string">&quot;Pizza Diavola&quot;</span>
                      <span class="name">url</span>: <span class="string">&quot;qrc:///pages/pizza.html&quot;</span>
                  }
                  <span class="type"><a href="../qtqml/qml-qtqml-models-listelement.html">ListElement</a></span> {
                      <span class="name">name</span>: <span class="string">&quot;Steak&quot;</span>
                      <span class="name">url</span>: <span class="string">&quot;qrc:///pages/steak.html&quot;</span>
                  }
                  <span class="type"><a href="../qtqml/qml-qtqml-models-listelement.html">ListElement</a></span> {
                      <span class="name">name</span>: <span class="string">&quot;Burger&quot;</span>
                      <span class="name">url</span>: <span class="string">&quot;qrc:///pages/burger.html&quot;</span>
                  }
                  <span class="type"><a href="../qtqml/qml-qtqml-models-listelement.html">ListElement</a></span> {
                      <span class="name">name</span>: <span class="string">&quot;Soup&quot;</span>
                      <span class="name">url</span>: <span class="string">&quot;qrc:///pages/soup.html&quot;</span>
                  }
                  <span class="type"><a href="../qtqml/qml-qtqml-models-listelement.html">ListElement</a></span> {
                      <span class="name">name</span>: <span class="string">&quot;Pasta&quot;</span>
                      <span class="name">url</span>: <span class="string">&quot;qrc:///pages/pasta.html&quot;</span>
                  }
                  <span class="type"><a href="../qtqml/qml-qtqml-models-listelement.html">ListElement</a></span> {
                      <span class="name">name</span>: <span class="string">&quot;Grilled Skewers&quot;</span>
                      <span class="name">url</span>: <span class="string">&quot;qrc:///pages/skewers.html&quot;</span>
                  }
                  <span class="type"><a href="../qtqml/qml-qtqml-models-listelement.html">ListElement</a></span> {
                      <span class="name">name</span>: <span class="string">&quot;Cupcakes&quot;</span>
                      <span class="name">url</span>: <span class="string">&quot;qrc:///pages/cupcakes.html&quot;</span>
                  }
              }

</pre>
<p>We use a <a href="../qtquickcontrols2/qml-qtquick-controls2-tooltip.html">ToolTip</a> item that is displayed on application startup to inform the users how they can navigate and view the details of each recipe. The <a href="../qtquickcontrols2/qml-qtquick-controls2-tooltip.html">ToolTip</a> is shown using the <code>showHelp</code> method, which is invoked by the <a href="../qtqml/qml-qtqml-timer.html">Timer</a> in the main.qml file.</p>
<pre class="cpp">

              <span class="type">ToolTip</span> {
                  <span class="name">id</span>: <span class="name">help</span>
                  <span class="name">implicitWidth</span>: <span class="name">root</span>.<span class="name">width</span> <span class="operator">-</span> <span class="name">padding</span> <span class="operator">*</span> <span class="number">3</span>
                  <span class="name">y</span>: <span class="name">root</span>.<span class="name">y</span> <span class="operator">+</span> <span class="name">root</span>.<span class="name">height</span>
                  <span class="name">delay</span>: <span class="number">1000</span>
                  <span class="name">timeout</span>: <span class="number">5000</span>
                  <span class="name">text</span>: <span class="name">qsTr</span>(<span class="string">&quot;Use keyboard, mouse, or touch controls to navigate through the\
                              recipes.&quot;</span>)

                  <span class="name">contentItem</span>: <span class="name">Text</span> {
                      <span class="name">text</span>: <span class="name">help</span>.<span class="name">text</span>
                      <span class="name">font</span>: <span class="name">help</span>.<span class="name">font</span>
                      <span class="name">color</span>: <span class="name">help</span>.<span class="name">Material</span>.<span class="name">primaryTextColor</span>
                      <span class="name">wrapMode</span>: <span class="name">Text</span>.<span class="name">Wrap</span>
                  }
              }
          }
      }

      <span class="keyword">function</span> <span class="name">showHelp</span>() {
          <span class="name">help</span>.<span class="name">open</span>()
      }
  }

</pre>
<p>An example of a recipe page can be seen below. The page uses two stylesheets and two JavaScript files:</p>
<ul>
<li><a href="http://kevinburke.bitbucket.org/markdowncss/">markdown.css</a> is a markdown-friendly stylesheet created by Kevin Burke</li>
<li><a href="https://github.com/chjj/marked">marked.min.js</a> is a markdown parser and compiler designed for speed written by Christopher Jeffrey</li>
<li>custom.css makes some small styling adjustments to the final recipe page</li>
<li>custom.js is used to invoke the conversion of the recipe content (which is written in markdown syntax) into HTML</li>
</ul>
<p>The images on the pages are loaded from the compiled resource file.</p>
<pre class="cpp">

  &lt;!doctype html&gt;
  &lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;title&gt;Soup&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;assets/3rdparty/markdown.css&quot;&gt;
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;assets/custom.css&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;placeholder&quot;&gt;&lt;/div&gt;
    &lt;div id=&quot;content&quot;&gt;

  &lt;img src=&quot;images/soup.jpg&quot; alt=&quot;Soup&quot; title=&quot;Soup&quot; /&gt;
  Soup
  =============

  ### Ingredients

  * 2 potatoes
  * 2 onions
  * 2 cloves of garlic
  * olive oil
  * 400 ml organic stock
  * 3 bunches of watercress

  ### Instructions
  * Peel and roughly chop the potatoes, onions and garlic.
  * In a large saucepan, heat a little olive oil, then sauté the potato, onion and garlic until the onions are translucent.
  * Add the stock and simmer until the potato is soft. Chop and add the watercress and simmer for a further 3 to 4 minutes.
  * Using a hand blender, liquidise the soup until smooth.
  * Serve with a swirl of crème fraîche and some Fortt’s Bath Oliver biscuits, if you like.

  **Enjoy!**

    &lt;/div&gt;&lt;!--End of content--&gt;
    &lt;script src=&quot;assets/3rdparty/marked.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;assets/custom.js&quot;&gt;&lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;

</pre>
<a name="files-and-attributions"></a>
<h2 id="files-and-attributions">Files and Attributions</h2>
<p>The example bundles the following code with third-party licenses:</p>
<div class="table"><table class="generic">
 <tr valign="top" class="odd"><td ><a href="qtwebengine-attribution-recipebrowser-marked.html#recipebrowser-marked">Marked</a></td><td >MIT License</td></tr>
<tr valign="top" class="even"><td ><a href="qtwebengine-attribution-recipebrowser-markdowncss.html#recipebrowser-markdowncss">Markdown.css</a></td><td >Apache License 2.0</td></tr>
</table></div>
<p>Files:</p>
<ul>
<li><a href="qtwebengine-webengine-recipebrowser-main-cpp.html">webengine/recipebrowser/main.cpp</a></li>
<li><a href="qtwebengine-webengine-recipebrowser-recipebrowser-pro.html">webengine/recipebrowser/recipebrowser.pro</a></li>
<li><a href="qtwebengine-webengine-recipebrowser-resources-pages-assets-3rdparty-markdown-css.html">webengine/recipebrowser/resources/pages/assets/3rdparty/markdown.css</a></li>
<li><a href="qtwebengine-webengine-recipebrowser-resources-pages-assets-3rdparty-marked-js.html">webengine/recipebrowser/resources/pages/assets/3rdparty/marked.js</a></li>
<li><a href="qtwebengine-webengine-recipebrowser-resources-pages-assets-custom-css.html">webengine/recipebrowser/resources/pages/assets/custom.css</a></li>
<li><a href="qtwebengine-webengine-recipebrowser-resources-pages-assets-custom-js.html">webengine/recipebrowser/resources/pages/assets/custom.js</a></li>
<li><a href="qtwebengine-webengine-recipebrowser-resources-pages-burger-html.html">webengine/recipebrowser/resources/pages/burger.html</a></li>
<li><a href="qtwebengine-webengine-recipebrowser-resources-pages-cupcakes-html.html">webengine/recipebrowser/resources/pages/cupcakes.html</a></li>
<li><a href="qtwebengine-webengine-recipebrowser-resources-pages-pasta-html.html">webengine/recipebrowser/resources/pages/pasta.html</a></li>
<li><a href="qtwebengine-webengine-recipebrowser-resources-pages-pizza-html.html">webengine/recipebrowser/resources/pages/pizza.html</a></li>
<li><a href="qtwebengine-webengine-recipebrowser-resources-pages-skewers-html.html">webengine/recipebrowser/resources/pages/skewers.html</a></li>
<li><a href="qtwebengine-webengine-recipebrowser-resources-pages-soup-html.html">webengine/recipebrowser/resources/pages/soup.html</a></li>
<li><a href="qtwebengine-webengine-recipebrowser-resources-pages-steak-html.html">webengine/recipebrowser/resources/pages/steak.html</a></li>
<li><a href="qtwebengine-webengine-recipebrowser-resources-qml-recipelist-qml.html">webengine/recipebrowser/resources/qml/RecipeList.qml</a></li>
<li><a href="qtwebengine-webengine-recipebrowser-resources-qml-main-qml.html">webengine/recipebrowser/resources/qml/main.qml</a></li>
<li><a href="qtwebengine-webengine-recipebrowser-resources-resources-qrc.html">webengine/recipebrowser/resources/resources.qrc</a></li>
</ul>
<p>Images:</p>
<ul>
<li><a href="images/used-in-examples/webengine/recipebrowser/resources/pages/images/burger.jpg">webengine/recipebrowser/resources/pages/images/burger.jpg</a></li>
<li><a href="images/used-in-examples/webengine/recipebrowser/resources/pages/images/cupcakes.jpg">webengine/recipebrowser/resources/pages/images/cupcakes.jpg</a></li>
<li><a href="images/used-in-examples/webengine/recipebrowser/resources/pages/images/pasta.jpg">webengine/recipebrowser/resources/pages/images/pasta.jpg</a></li>
<li><a href="images/used-in-examples/webengine/recipebrowser/resources/pages/images/pizza.jpg">webengine/recipebrowser/resources/pages/images/pizza.jpg</a></li>
<li><a href="images/used-in-examples/webengine/recipebrowser/resources/pages/images/skewers.jpg">webengine/recipebrowser/resources/pages/images/skewers.jpg</a></li>
<li><a href="images/used-in-examples/webengine/recipebrowser/resources/pages/images/soup.jpg">webengine/recipebrowser/resources/pages/images/soup.jpg</a></li>
<li><a href="images/used-in-examples/webengine/recipebrowser/resources/pages/images/steak.jpg">webengine/recipebrowser/resources/pages/images/steak.jpg</a></li>
</ul>
</div>
<!-- @@@webengine/recipebrowser -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2018 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>