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  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QDeclarativeContext Class Reference

The QDeclarativeContext class defines a context within a QML engine. More...

 #include <QDeclarativeContext>

Inherits: QObject.

This class was introduced in Qt 4.7.

Public Functions

QDeclarativeContext ( QDeclarativeEngine * engine, QObject * parent = 0 )
QDeclarativeContext ( QDeclarativeContext * parentContext, QObject * parent = 0 )
virtual ~QDeclarativeContext ()
QUrl baseUrl () const
QObject * contextObject () const
QVariant contextProperty ( const QString & name ) const
QDeclarativeEngine * engine () const
bool isValid () const
QDeclarativeContext * parentContext () const
QUrl resolvedUrl ( const QUrl & src )
void setBaseUrl ( const QUrl & baseUrl )
void setContextObject ( QObject * object )
void setContextProperty ( const QString & name, QObject * value )
void setContextProperty ( const QString & name, const QVariant & value )
  • 29 public functions inherited from QObject

Additional Inherited Members

  • 1 property inherited from QObject
  • 1 public slot inherited from QObject
  • 1 signal inherited from QObject
  • 5 static public members inherited from QObject
  • 7 protected functions inherited from QObject

Detailed Description

The QDeclarativeContext class defines a context within a QML engine.

Contexts allow data to be exposed to the QML components instantiated by the QML engine.

Each QDeclarativeContext contains a set of properties, distinct from its QObject properties, that allow data to be explicitly bound to a context by name. The context properties are defined and updated by calling QDeclarativeContext::setContextProperty(). The following example shows a Qt model being bound to a context and then accessed from a QML file.

 QDeclarativeEngine engine;
 QStringListModel modelData;
 QDeclarativeContext *context = new QDeclarativeContext(engine.rootContext());
 context->setContextProperty("myModel", &modelData);

 QDeclarativeComponent component(&engine);
 component.setData("import QtQuick 1.0\nListView { model: myModel }", QUrl());
 QObject *window = component.create(context);

Note it is the responsibility of the creator to delete any QDeclarativeContext it constructs. If the context object in the example is no longer needed when the window component instance is destroyed, the context must be destroyed explicitly. The simplest way to ensure this is to set window as the parent of context.

To simplify binding and maintaining larger data sets, a context object can be set on a QDeclarativeContext. All the properties of the context object are available by name in the context, as though they were all individually added through calls to QDeclarativeContext::setContextProperty(). Changes to the property's values are detected through the property's notify signal. Setting a context object is both faster and easier than manually adding and maintaing context property values.

The following example has the same effect as the previous one, but it uses a context object.

 class MyDataSet : ... {
     ...
     Q_PROPERTY(QAbstractItemModel *myModel READ model NOTIFY modelChanged)
     ...
 };

 MyDataSet myDataSet;
 QDeclarativeEngine engine;
 QDeclarativeContext *context = new QDeclarativeContext(engine.rootContext());
 context->setContextObject(&myDataSet);

 QDeclarativeComponent component(&engine);
 component.setData("import QtQuick 1.0\nListView { model: myModel }", QUrl());
 component.create(context);

All properties added explicitly by QDeclarativeContext::setContextProperty() take precedence over the context object's properties.

The Context Hierarchy

Contexts form a hierarchy. The root of this hierarchy is the QML engine's root context. Child contexts inherit the context properties of their parents; if a child context sets a context property that already exists in its parent, the new context property overrides that of the parent.

The following example defines two contexts - context1 and context2. The second context overrides the "b" context property inherited from the first with a new value.

 QDeclarativeEngine engine;
 QDeclarativeContext *context1 = new QDeclarativeContext(engine.rootContext());
 QDeclarativeContext *context2 = new QDeclarativeContext(context1);

 context1->setContextProperty("a", 12);
 context1->setContextProperty("b", 12);

 context2->setContextProperty("b", 15);

While QML objects instantiated in a context are not strictly owned by that context, their bindings are. If a context is destroyed, the property bindings of outstanding QML objects will stop evaluating.

Warning: Setting the context object or adding new context properties after an object has been created in that context is an expensive operation (essentially forcing all bindings to reevaluate). Thus whenever possible you should complete "setup" of the context before using it to create any objects.

See also Using QML Bindings in C++ Applications.

Member Function Documentation

QDeclarativeContext::QDeclarativeContext ( QDeclarativeEngine * engine, QObject * parent = 0 )

Create a new QDeclarativeContext as a child of engine's root context, and the QObject parent.

QDeclarativeContext::QDeclarativeContext ( QDeclarativeContext * parentContext, QObject * parent = 0 )

Create a new QDeclarativeContext with the given parentContext, and the QObject parent.

QDeclarativeContext::~QDeclarativeContext () [virtual]

Destroys the QDeclarativeContext.

Any expressions, or sub-contexts dependent on this context will be invalidated, but not destroyed (unless they are parented to the QDeclarativeContext object).

QUrl QDeclarativeContext::baseUrl () const

Returns the base url of the component, or the containing component if none is set.

See also setBaseUrl().

QObject * QDeclarativeContext::contextObject () const

Return the context object, or 0 if there is no context object.

See also setContextObject().

QVariant QDeclarativeContext::contextProperty ( const QString & name ) const

Returns the value of the name property for this context as a QVariant.

See also setContextProperty().

QDeclarativeEngine * QDeclarativeContext::engine () const

Return the context's QDeclarativeEngine, or 0 if the context has no QDeclarativeEngine or the QDeclarativeEngine was destroyed.

bool QDeclarativeContext::isValid () const

Returns whether the context is valid.

To be valid, a context must have a engine, and it's contextObject(), if any, must not have been deleted.

QDeclarativeContext * QDeclarativeContext::parentContext () const

Return the context's parent QDeclarativeContext, or 0 if this context has no parent or if the parent has been destroyed.

QUrl QDeclarativeContext::resolvedUrl ( const QUrl & src )

Resolves the URL src relative to the URL of the containing component.

See also QDeclarativeEngine::baseUrl() and setBaseUrl().

void QDeclarativeContext::setBaseUrl ( const QUrl & baseUrl )

Explicitly sets the url resolvedUrl() will use for relative references to baseUrl.

Calling this function will override the url of the containing component used by default.

See also baseUrl() and resolvedUrl().

void QDeclarativeContext::setContextObject ( QObject * object )

Set the context object.

See also contextObject().

void QDeclarativeContext::setContextProperty ( const QString & name, QObject * value )

Set the value of the name property on this context.

QDeclarativeContext does not take ownership of value.

See also contextProperty().

void QDeclarativeContext::setContextProperty ( const QString & name, const QVariant & value )

Set a the value of the name property on this context.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 78
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 6
Page suivante

Le Qt Quarterly au hasard

Logo

Les aventures de QLALR - utiliser QLALR pour générer un parseur pour une aventure textuelle

Qt Quarterly est la revue trimestrielle proposée par Nokia et à destination des développeurs Qt. Ces articles d'une grande qualité technique sont rédigés par des experts Qt. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

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 4.7-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