Files
qt4-x11/doc/html/qml-integration.html
2025-08-24 20:55:26 +08:00

258 lines
16 KiB
HTML

<?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/style.css" />
<script src="scripts/jquery.js" type="text/javascript"></script>
<script src="scripts/functions.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="style/superfish.css" />
<link rel="stylesheet" type="text/css" href="style/narrow.css" />
<!--[if IE]>
<meta name="MSSmartTagsPreventParsing" content="true">
<meta http-equiv="imagetoolbar" content="no">
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
<![endif]-->
<script src="scripts/superfish.js" type="text/javascript"></script>
<script src="scripts/narrow.js" type="text/javascript"></script>
</head>
<body class="" onload="CheckEmptyAndLoadList();">
<div class="header" id="qtdocheader">
<div class="content">
<div id="nav-logo">
<a href="index.html">Home</a></div>
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
<div id="narrowsearch"></div>
<div id="nav-topright">
<ul>
<li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li>
<li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li>
<li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/">
DOC</a></li>
<li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li>
</ul>
</div>
<div id="shortCut">
<ul>
<li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li>
<li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS </a></li>
</ul>
</div>
<ul class="sf-menu" id="narrowmenu">
<li><a href="#">API Lookup</a>
<ul>
<li><a href="classes.html">Class index</a></li>
<li><a href="functions.html">Function index</a></li>
<li><a href="modules.html">Modules</a></li>
<li><a href="namespaces.html">Namespaces</a></li>
<li><a href="qtglobal.html">Global Declarations</a></li>
<li><a href="qdeclarativeelements.html">QML elements</a></li>
</ul>
</li>
<li><a href="#">Qt Topics</a>
<ul>
<li><a href="qt-basic-concepts.html">Programming with Qt</a></li>
<li><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li>
<li><a href="qt-gui-concepts.html">UI Design with Qt</a></li>
<li><a href="supported-platforms.html">Supported Platforms</a></li>
<li><a href="technology-apis.html">Qt and Key Technologies</a></li>
<li><a href="best-practices.html">How-To's and Best Practices</a></li>
</ul>
</li>
<li><a href="#">Examples</a>
<ul>
<li><a href="all-examples.html">Examples</a></li>
<li><a href="tutorials.html">Tutorials</a></li>
<li><a href="demos.html">Demos</a></li>
<li><a href="qdeclarativeexamples.html">QML Examples</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="wrapper">
<div class="hd">
<span></span>
</div>
<div class="bd group">
<div class="sidebar">
<div class="searchlabel">
Search index:</div>
<div class="search" id="sidebarsearch">
<form id="qtdocsearch" action="" onsubmit="return false;">
<fieldset>
<input type="text" name="searchstring" id="pageType" value="" />
<div id="resultdialog">
<a href="#" id="resultclose">Close</a>
<p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p>
<p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span>&nbsp;results:</p>
<ul id="resultlist" class="all">
</ul>
</div>
</fieldset>
</form>
</div>
<div class="box first bottombar" id="lookup">
<h2 title="API Lookup"><span></span>
API Lookup</h2>
<div id="list001" class="list">
<ul id="ul001" >
<li class="defaultLink"><a href="classes.html">Class index</a></li>
<li class="defaultLink"><a href="functions.html">Function index</a></li>
<li class="defaultLink"><a href="modules.html">Modules</a></li>
<li class="defaultLink"><a href="namespaces.html">Namespaces</a></li>
<li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li>
<li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li>
</ul>
</div>
</div>
<div class="box bottombar" id="topics">
<h2 title="Qt Topics"><span></span>
Qt Topics</h2>
<div id="list002" class="list">
<ul id="ul002" >
<li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li>
<li class="defaultLink"><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li>
<li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li>
<li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li>
<li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li>
<li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li>
</ul>
</div>
</div>
<div class="box" id="examples">
<h2 title="Examples"><span></span>
Examples</h2>
<div id="list003" class="list">
<ul id="ul003">
<li class="defaultLink"><a href="all-examples.html">Examples</a></li>
<li class="defaultLink"><a href="tutorials.html">Tutorials</a></li>
<li class="defaultLink"><a href="demos.html">Demos</a></li>
<li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li>
</ul>
</div>
</div>
</div>
<div class="wrap">
<div class="toolbar">
<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 class="toolbuttons toolblock">
<ul>
<li id="smallA" class="t_button">A</li>
<li id="medA" class="t_button active">A</li>
<li id="bigA" class="t_button">A</li>
<li id="print" class="t_button"><a href="javascript:this.print();">
<span>Print</span></a></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">&gt;</span>setSource(<span class="type"><a href="qurl.html">QUrl</a></span><span class="operator">::</span>fromLocalFile(<span class="string">&quot;myqml.qml&quot;</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">&gt;</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">&quot;myqml.qml&quot;</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">&lt;</span><span class="type"><a href="qgraphicsobject.html">QGraphicsObject</a></span> <span class="operator">*</span><span class="operator">&gt;</span>(component<span class="operator">.</span>create());
scene<span class="operator">-</span><span class="operator">&gt;</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>
</div>
</div>
<div class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">&copy;</acronym> 2015 The Qt Company Ltd.
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>
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. <a title="Privacy Policy"
href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
<script src="scripts/functions.js" type="text/javascript"></script>
</body>
</html>