source: trunk/doc/src/declarative/integrating.qdoc@ 1147

Last change on this file since 1147 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 4.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:FDL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in a
14** written agreement between you and Nokia.
15**
16** GNU Free Documentation License
17** Alternatively, this file may be used under the terms of the GNU Free
18** Documentation License version 1.3 as published by the Free Software
19** Foundation and appearing in the file included in the packaging of this
20** file.
21**
22** If you have questions regarding the use of this file, please contact
23** Nokia at qt-info@nokia.com.
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29\page qml-integration.html
30\title Integrating QML with existing Qt UI code
31
32There are a number of ways to integrate QML into QWidget-based UI applications,
33depending on the characteristics of your existing UI code.
34
35
36\section1 Integrating with a \l{QWidget}-based UI
37
38If you have an existing QWidget-based UI, QML widgets can be integrated into
39it using QDeclarativeView. QDeclarativeView is a subclass of QWidget so you
40can add it to your user interface like any other QWidget. Use
41QDeclarativeView::setSource() to load a QML file into the view, then add the
42view to your UI:
43
44\code
45QDeclarativeView *qmlView = new QDeclarativeView;
46qmlView->setSource(QUrl::fromLocalFile("myqml.qml"));
47
48QWidget *widget = myExistingWidget();
49QVBoxLayout *layout = new QVBoxLayout(widget);
50layout->addWidget(qmlView);
51\endcode
52
53The one drawback to this approach is that QDeclarativeView is slower to initialize
54and uses more memory than a QWidget, and creating large numbers of QDeclarativeView
55objects may lead to performance degradation. If this is the case, it may be
56better to rewrite your widgets in QML, and load the widgets from a main QML widget
57instead of using QDeclarativeView.
58
59Keep in mind that QWidgets were designed for a different type of user interface
60than QML, so it is not always a good idea to port a QWidget-based application to
61QML. QWidgets are a better choice if your UI is comprised of a small number of
62complex and static elements, and QML is a better choice if your UI is comprised of a large number
63of simple and dynamic elements.
64
65
66\section1 Integrating with a QGraphicsView-based UI
67
68\section2 Adding QML widgets to a QGraphicsScene
69
70If you have an existing UI based on the \l{Graphics View Framework},
71you can integrate QML widgets directly into your QGraphicsScene. Use
72QDeclarativeComponent to create a QGraphicsObject from a QML file, and
73place the graphics object into your scene using \l{QGraphicsScene::addItem()}, or
74reparent it to an item already in the \l{QGraphicsScene}.
75
76For example:
77
78\code
79QGraphicsScene* scene = myExistingGraphicsScene();
80QDeclarativeEngine *engine = new QDeclarativeEngine;
81QDeclarativeComponent component(engine, QUrl::fromLocalFile("myqml.qml"));
82QGraphicsObject *object =
83 qobject_cast<QGraphicsObject *>(component.create());
84scene->addItem(object);
85\endcode
86
87The following QGraphicsView options are recommended for optimal performance
88of QML UIs:
89
90\list
91\o QGraphicsView::setOptimizationFlags(QGraphicsView::DontSavePainterState)
92\o QGraphicsView::setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate)
93\o QGraphicsScene::setItemIndexMethod(QGraphicsScene::NoIndex)
94\endlist
95
96\section2 Loading QGraphicsWidget objects in QML
97
98An alternative approach is to expose your existing QGraphicsWidget objects to
99QML and construct your scene in QML instead. See the \l {declarative-cppextensions-qgraphicslayouts.html}{graphics layouts example}
100which shows how to expose Qt's graphics layout classes to QML in order
101to use QGraphicsWidget with classes like QGraphicsLinearLayout and QGraphicsGridLayout.
102
103To expose your existing QGraphicsWidget classes to QML, use \l {qmlRegisterType()}.
104See \l{Extending QML in C++} for further information on using C++ types in QML.
105
106*/
Note: See TracBrowser for help on using the repository browser.