Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

Integrating QML Code with Existing Qt UI Code

There are a number of ways to integrate QML into QWidget-based UI applications, depending on the characteristics of your existing UI code.

Integrating with a QWidget-based UI

If you have an existing QWidget-based UI, QML widgets can be integrated into it using QQuickView. QQuickView is a subclass of QWidget so you can add it to your user interface like any other QWidget. Use QQuickView::setSource() to load a QML file into the view, then add the view to your UI:

 QQuickView *qmlView = new QQuickView;
 qmlView->setSource(QUrl::fromLocalFile("myqml.qml"));

 QWidget *widget = myExistingWidget();
 QVBoxLayout *layout = new QVBoxLayout(widget);
 layout->addWidget(qmlView);

The one drawback to this approach is that QQuickView is slower to initialize and uses more memory than a QWidget, and creating large numbers of QQuickView 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 QQuickView.

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 QWidget-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.

Integrating with a QGraphicsView-based UI

Adding QML widgets to a QGraphicsScene

If you have an existing UI based on the Graphics View Framework, you can integrate QML widgets directly into your QGraphicsScene. Use QQmlComponent to create a QGraphicsObject from a QML file, and place the graphics object into your scene using QGraphicsScene::addItem(), or reparent it to an item already in the QGraphicsScene.

For example:

 QGraphicsScene* scene = myExistingGraphicsScene();
 QQmlEngine *engine = new QQmlEngine;
 QQmlComponent component(engine, QUrl::fromLocalFile("myqml.qml"));
 QGraphicsObject *object =
     qobject_cast<QGraphicsObject *>(component.create());
 scene->addItem(object);

The following QGraphicsView options are recommended for optimal performance of QML UIs:

Loading QGraphicsWidget Objects in QML

An alternative approach is to expose your existing QGraphicsWidget objects to QML and construct your scene in QML instead. See the graphics layouts example which shows how to expose Qt's graphics layout classes to QML in order to use QGraphicsWidget with classes like QGraphicsLinearLayout and QGraphicsGridLayout.

To expose your existing QGraphicsWidget classes to QML, use qmlRegisterType(). See Extending QML with C++ for further information on how to use C++ types in QML.

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web