src/declarative/qml/qdeclarativecontext.cpp
changeset 37 758a864f9613
parent 33 3e2da88830cd
equal deleted inserted replaced
36:ef0373b55136 37:758a864f9613
    78     QDeclarativeContext::setContextProperty().  The following example shows a Qt model 
    78     QDeclarativeContext::setContextProperty().  The following example shows a Qt model 
    79     being bound to a context and then accessed from a QML file.
    79     being bound to a context and then accessed from a QML file.
    80 
    80 
    81     \code
    81     \code
    82     QDeclarativeEngine engine;
    82     QDeclarativeEngine engine;
       
    83     QStringListModel modelData;
    83     QDeclarativeContext *context = new QDeclarativeContext(engine.rootContext());
    84     QDeclarativeContext *context = new QDeclarativeContext(engine.rootContext());
    84     context->setContextProperty("myModel", modelData);
    85     context->setContextProperty("myModel", &modelData);
    85 
    86 
    86     QDeclarativeComponent component(&engine, "ListView { model=myModel }");
    87     QDeclarativeComponent component(&engine);
       
    88     component.setData("import Qt 4.7\nListView { model: myModel }", QUrl());
    87     component.create(context);
    89     component.create(context);
    88     \endcode
    90     \endcode
    89 
    91 
    90     To simplify binding and maintaining larger data sets, a context object can be set
    92     To simplify binding and maintaining larger data sets, a context object can be set
    91     on a QDeclarativeContext.  All the properties of the context object are available
    93     on a QDeclarativeContext.  All the properties of the context object are available
   102         ...
   104         ...
   103         Q_PROPERTY(QAbstractItemModel *myModel READ model NOTIFY modelChanged)
   105         Q_PROPERTY(QAbstractItemModel *myModel READ model NOTIFY modelChanged)
   104         ...
   106         ...
   105     };
   107     };
   106 
   108 
   107     MyDataSet *myDataSet = new MyDataSet;
   109     MyDataSet myDataSet;
   108     QDeclarativeEngine engine;
   110     QDeclarativeEngine engine;
   109     QDeclarativeContext *context = new QDeclarativeContext(engine.rootContext());
   111     QDeclarativeContext *context = new QDeclarativeContext(engine.rootContext());
   110     context->setContextObject(myDataSet);
   112     context->setContextObject(&myDataSet);
   111 
   113 
   112     QDeclarativeComponent component(&engine, "ListView { model=myModel }");
   114     QDeclarativeComponent component(&engine);
       
   115     component.setData("import Qt 4.7\nListView { model: myModel }", QUrl());
   113     component.create(context);
   116     component.create(context);
   114     \endcode
   117     \endcode
   115 
   118 
   116     All properties added explicitly by QDeclarativeContext::setContextProperty() take 
   119     All properties added explicitly by QDeclarativeContext::setContextProperty() take 
   117     precedence over the context object's properties.
   120     precedence over the context object's properties.
   528 
   531 
   529     engine = 0;
   532     engine = 0;
   530     parent = 0;
   533     parent = 0;
   531 }
   534 }
   532 
   535 
   533 void QDeclarativeContextData::clearExpressions()
   536 void QDeclarativeContextData::clearContext()
   534 {
   537 {
       
   538     if (engine) {
       
   539         while (componentAttached) {
       
   540             QDeclarativeComponentAttached *a = componentAttached;
       
   541             componentAttached = a->next;
       
   542             if (componentAttached) componentAttached->prev = &componentAttached;
       
   543 
       
   544             a->next = 0;
       
   545             a->prev = 0;
       
   546 
       
   547             emit a->destruction();
       
   548         }
       
   549     }
       
   550 
   535     QDeclarativeAbstractExpression *expression = expressions;
   551     QDeclarativeAbstractExpression *expression = expressions;
   536     while (expression) {
   552     while (expression) {
   537         QDeclarativeAbstractExpression *nextExpression = expression->m_nextExpression;
   553         QDeclarativeAbstractExpression *nextExpression = expression->m_nextExpression;
   538 
   554 
   539         expression->m_context = 0;
   555         expression->m_context = 0;
   550     if (linkedContext) 
   566     if (linkedContext) 
   551         linkedContext->destroy();
   567         linkedContext->destroy();
   552 
   568 
   553     if (engine) invalidate();
   569     if (engine) invalidate();
   554 
   570 
   555     clearExpressions();
   571     clearContext();
   556 
   572 
   557     while (contextObjects) {
   573     while (contextObjects) {
   558         QDeclarativeData *co = contextObjects;
   574         QDeclarativeData *co = contextObjects;
   559         contextObjects = contextObjects->nextContextObject;
   575         contextObjects = contextObjects->nextContextObject;
   560 
   576 
   642 void QDeclarativeContextData::addImportedScript(const QDeclarativeParser::Object::ScriptBlock &script)
   658 void QDeclarativeContextData::addImportedScript(const QDeclarativeParser::Object::ScriptBlock &script)
   643 {
   659 {
   644     if (!engine) 
   660     if (!engine) 
   645         return;
   661         return;
   646 
   662 
   647     Q_ASSERT(script.codes.count() == 1);
       
   648 
       
   649     QDeclarativeEnginePrivate *enginePriv = QDeclarativeEnginePrivate::get(engine);
   663     QDeclarativeEnginePrivate *enginePriv = QDeclarativeEnginePrivate::get(engine);
   650     QScriptEngine *scriptEngine = QDeclarativeEnginePrivate::getScriptEngine(engine);
   664     QScriptEngine *scriptEngine = QDeclarativeEnginePrivate::getScriptEngine(engine);
   651 
   665 
   652     const QString &code = script.codes.at(0);
   666     const QString &code = script.code;
   653     const QString &url = script.files.at(0);
   667     const QString &url = script.file;
   654     const QDeclarativeParser::Object::ScriptBlock::Pragmas &pragmas = script.pragmas.at(0);
   668     const QDeclarativeParser::Object::ScriptBlock::Pragmas &pragmas = script.pragmas;
   655 
   669 
   656     Q_ASSERT(!url.isEmpty());
   670     Q_ASSERT(!url.isEmpty());
   657 
   671 
   658     if (pragmas & QDeclarativeParser::Object::ScriptBlock::Shared) {
   672     if (pragmas & QDeclarativeParser::Object::ScriptBlock::Shared) {
   659 
   673