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 |
|
---|
32 | There are a number of ways to integrate QML into QWidget-based UI applications,
|
---|
33 | depending on the characteristics of your existing UI code.
|
---|
34 |
|
---|
35 |
|
---|
36 | \section1 Integrating with a \l{QWidget}-based UI
|
---|
37 |
|
---|
38 | If you have an existing QWidget-based UI, QML widgets can be integrated into
|
---|
39 | it using QDeclarativeView. QDeclarativeView is a subclass of QWidget so you
|
---|
40 | can add it to your user interface like any other QWidget. Use
|
---|
41 | QDeclarativeView::setSource() to load a QML file into the view, then add the
|
---|
42 | view to your UI:
|
---|
43 |
|
---|
44 | \code
|
---|
45 | QDeclarativeView *qmlView = new QDeclarativeView;
|
---|
46 | qmlView->setSource(QUrl::fromLocalFile("myqml.qml"));
|
---|
47 |
|
---|
48 | QWidget *widget = myExistingWidget();
|
---|
49 | QVBoxLayout *layout = new QVBoxLayout(widget);
|
---|
50 | layout->addWidget(qmlView);
|
---|
51 | \endcode
|
---|
52 |
|
---|
53 | The one drawback to this approach is that QDeclarativeView is slower to initialize
|
---|
54 | and uses more memory than a QWidget, and creating large numbers of QDeclarativeView
|
---|
55 | objects may lead to performance degradation. If this is the case, it may be
|
---|
56 | better to rewrite your widgets in QML, and load the widgets from a main QML widget
|
---|
57 | instead of using QDeclarativeView.
|
---|
58 |
|
---|
59 | Keep in mind that QWidgets were designed for a different type of user interface
|
---|
60 | than QML, so it is not always a good idea to port a QWidget-based application to
|
---|
61 | QML. QWidgets are a better choice if your UI is comprised of a small number of
|
---|
62 | complex and static elements, and QML is a better choice if your UI is comprised of a large number
|
---|
63 | of simple and dynamic elements.
|
---|
64 |
|
---|
65 |
|
---|
66 | \section1 Integrating with a QGraphicsView-based UI
|
---|
67 |
|
---|
68 | \section2 Adding QML widgets to a QGraphicsScene
|
---|
69 |
|
---|
70 | If you have an existing UI based on the \l{Graphics View Framework},
|
---|
71 | you can integrate QML widgets directly into your QGraphicsScene. Use
|
---|
72 | QDeclarativeComponent to create a QGraphicsObject from a QML file, and
|
---|
73 | place the graphics object into your scene using \l{QGraphicsScene::addItem()}, or
|
---|
74 | reparent it to an item already in the \l{QGraphicsScene}.
|
---|
75 |
|
---|
76 | For example:
|
---|
77 |
|
---|
78 | \code
|
---|
79 | QGraphicsScene* scene = myExistingGraphicsScene();
|
---|
80 | QDeclarativeEngine *engine = new QDeclarativeEngine;
|
---|
81 | QDeclarativeComponent component(engine, QUrl::fromLocalFile("myqml.qml"));
|
---|
82 | QGraphicsObject *object =
|
---|
83 | qobject_cast<QGraphicsObject *>(component.create());
|
---|
84 | scene->addItem(object);
|
---|
85 | \endcode
|
---|
86 |
|
---|
87 | The following QGraphicsView options are recommended for optimal performance
|
---|
88 | of 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 |
|
---|
98 | An alternative approach is to expose your existing QGraphicsWidget objects to
|
---|
99 | QML and construct your scene in QML instead. See the \l {declarative-cppextensions-qgraphicslayouts.html}{graphics layouts example}
|
---|
100 | which shows how to expose Qt's graphics layout classes to QML in order
|
---|
101 | to use QGraphicsWidget with classes like QGraphicsLinearLayout and QGraphicsGridLayout.
|
---|
102 |
|
---|
103 | To expose your existing QGraphicsWidget classes to QML, use \l {qmlRegisterType()}.
|
---|
104 | See \l{Extending QML in C++} for further information on using C++ types in QML.
|
---|
105 |
|
---|
106 | */
|
---|