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
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- integrating.qdoc -->
<title>Qt 4.8: Integrating QML Code with Existing Qt UI Code</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="content">
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
</div>
<div class="breadcrumb toolblock">
<ul>
<li class="first"><a href="index.html">Home</a></li>
<!-- Breadcrumbs go here -->
<li>Integrating QML Code with Existing Qt UI Code</li>
</ul>
</div>
</div>
<div class="content mainContent">
<link rel="prev" href="qtbinding.html" />
<link rel="next" href="qdeclarativedynamicobjects.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="qtbinding.html">Using QML Bindings in C++ Applications</a>
<a class="nextPage" href="qdeclarativedynamicobjects.html">Dynamic Object Management</a>
</p><p/>
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#integrating-with-a-qwidget-based-ui">Integrating with a <a href="qwidget.html">QWidget</a>-based UI</a></li>
<li class="level1"><a href="#integrating-with-a-qgraphicsview-based-ui">Integrating with a QGraphicsView-based UI</a></li>
<li class="level2"><a href="#adding-qml-widgets-to-a-qgraphicsscene">Adding QML widgets to a QGraphicsScene</a></li>
<li class="level2"><a href="#loading-qgraphicswidget-objects-in-qml">Loading QGraphicsWidget objects in QML</a></li>
</ul>
</div>
<h1 class="title">Integrating QML Code with Existing Qt UI Code</h1>
<span class="subtitle"></span>
<!-- $$$qml-integration.html-description -->
<div class="descr"> <a name="details"></a>
<p>There are a number of ways to integrate QML into <a href="qwidget.html">QWidget</a>-based UI applications, depending on the characteristics of your existing UI code.</p>
<a name="integrating-with-a-qwidget-based-ui"></a>
<h2>Integrating with a <a href="qwidget.html">QWidget</a>-based UI</h2>
<p>If you have an existing <a href="qwidget.html">QWidget</a>-based UI, QML widgets can be integrated into it using <a href="qdeclarativeview.html">QDeclarativeView</a>. <a href="qdeclarativeview.html">QDeclarativeView</a> is a subclass of <a href="qwidget.html">QWidget</a> so you can add it to your user interface like any other <a href="qwidget.html">QWidget</a>. Use <a href="qdeclarativeview.html#source-prop">QDeclarativeView::setSource</a>() to load a QML file into the view, then add the view to your UI:</p>
<pre class="cpp"> <span class="type"><a href="qdeclarativeview.html">QDeclarativeView</a></span> <span class="operator">*</span>qmlView <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qdeclarativeview.html">QDeclarativeView</a></span>;
qmlView<span class="operator">-</span><span class="operator">></span>setSource(<span class="type"><a href="qurl.html">QUrl</a></span><span class="operator">::</span>fromLocalFile(<span class="string">"myqml.qml"</span>));
<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>widget <span class="operator">=</span> myExistingWidget();
<span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>(widget);
layout<span class="operator">-</span><span class="operator">></span>addWidget(qmlView);</pre>
<p>The one drawback to this approach is that <a href="qdeclarativeview.html">QDeclarativeView</a> is slower to initialize and uses more memory than a <a href="qwidget.html">QWidget</a>, and creating large numbers of <a href="qdeclarativeview.html">QDeclarativeView</a> objects may lead to performance degradation. If this is the case, it may be better to rewrite your widgets in QML, and load the widgets from a main QML widget instead of using <a href="qdeclarativeview.html">QDeclarativeView</a>.</p>
<p>Keep in mind that QWidgets were designed for a different type of user interface than QML, so it is not always a good idea to port a <a href="qwidget.html">QWidget</a>-based application to QML. QWidgets are a better choice if your UI is comprised of a small number of complex and static elements, and QML is a better choice if your UI is comprised of a large number of simple and dynamic elements.</p>
<a name="integrating-with-a-qgraphicsview-based-ui"></a>
<h2>Integrating with a QGraphicsView-based UI</h2>
<a name="adding-qml-widgets-to-a-qgraphicsscene"></a>
<h3>Adding QML widgets to a QGraphicsScene</h3>
<p>If you have an existing UI based on the <a href="graphicsview.html">Graphics View Framework</a>, you can integrate QML widgets directly into your <a href="qgraphicsscene.html">QGraphicsScene</a>. Use <a href="qdeclarativecomponent.html">QDeclarativeComponent</a> to create a <a href="qgraphicsobject.html">QGraphicsObject</a> from a QML file, and place the graphics object into your scene using <a href="qgraphicsscene.html#addItem">QGraphicsScene::addItem</a>(), or reparent it to an item already in the <a href="qgraphicsscene.html">QGraphicsScene</a>.</p>
<p>For example:</p>
<pre class="cpp"> <span class="type"><a href="qgraphicsscene.html">QGraphicsScene</a></span><span class="operator">*</span> scene <span class="operator">=</span> myExistingGraphicsScene();
<span class="type"><a href="qdeclarativeengine.html">QDeclarativeEngine</a></span> <span class="operator">*</span>engine <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qdeclarativeengine.html">QDeclarativeEngine</a></span>;
<span class="type"><a href="qdeclarativecomponent.html">QDeclarativeComponent</a></span> component(engine<span class="operator">,</span> <span class="type"><a href="qurl.html">QUrl</a></span><span class="operator">::</span>fromLocalFile(<span class="string">"myqml.qml"</span>));
<span class="type"><a href="qgraphicsobject.html">QGraphicsObject</a></span> <span class="operator">*</span>object <span class="operator">=</span>
qobject_cast<span class="operator"><</span><span class="type"><a href="qgraphicsobject.html">QGraphicsObject</a></span> <span class="operator">*</span><span class="operator">></span>(component<span class="operator">.</span>create());
scene<span class="operator">-</span><span class="operator">></span>addItem(object);</pre>
<p>The following <a href="qgraphicsview.html">QGraphicsView</a> options are recommended for optimal performance of QML UIs:</p>
<ul>
<li>QGraphicsView::setOptimizationFlags(<a href="qgraphicsview.html#OptimizationFlag-enum">QGraphicsView::DontSavePainterState</a>)</li>
<li>QGraphicsView::setViewportUpdateMode(<a href="qgraphicsview.html#ViewportUpdateMode-enum">QGraphicsView::BoundingRectViewportUpdate</a>)</li>
<li>QGraphicsScene::setItemIndexMethod(<a href="qgraphicsscene.html#ItemIndexMethod-enum">QGraphicsScene::NoIndex</a>)</li>
</ul>
<a name="loading-qgraphicswidget-objects-in-qml"></a>
<h3>Loading QGraphicsWidget objects in QML</h3>
<p>An alternative approach is to expose your existing <a href="qgraphicswidget.html">QGraphicsWidget</a> objects to QML and construct your scene in QML instead. See the <a href="declarative-cppextensions-qgraphicslayouts.html">graphics layouts example</a> which shows how to expose Qt's graphics layout classes to QML in order to use <a href="qgraphicswidget.html">QGraphicsWidget</a> with classes like <a href="qgraphicslinearlayout.html">QGraphicsLinearLayout</a> and <a href="qgraphicsgridlayout.html">QGraphicsGridLayout</a>.</p>
<p>To expose your existing <a href="qgraphicswidget.html">QGraphicsWidget</a> classes to QML, use <a href="qdeclarativeengine.html#qmlRegisterType">qmlRegisterType</a>(). See <a href="qml-extending.html">Extending QML Functionalities using C++</a> for further information on how to use C++ types in QML.</p>
</div>
<!-- @@@qml-integration.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="qtbinding.html">Using QML Bindings in C++ Applications</a>
<a class="nextPage" href="qdeclarativedynamicobjects.html">Dynamic Object Management</a>
</p>
<div class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2012 Nokia Corporation and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.</p>
<br />
<p>
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.</p>
<p>
Documentation sources may be obtained from <a href="http://www.qt-project.org">
www.qt-project.org</a>.</p>
<br />
<p>
Nokia, Qt and their respective logos are trademarks of Nokia Corporation
in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. <a title="Privacy Policy"
href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>
|