Provide a context when constructing a TextInput cursor delegate.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Fri, 30 Sep 2011 07:17:49 +0000 (17:17 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 12 Oct 2011 02:38:36 +0000 (04:38 +0200)
Without a context the delegate won't be able to refer to any external
properties.

Task-number: QTBUG-21780
Change-Id: I7171787e677ce67466b311796693ed88bcacb718
Reviewed-on: http://codereview.qt-project.org/5837
Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Aaron Kennedy <aaron.kennedy@nokia.com>
src/declarative/items/qsgtextedit.cpp
src/declarative/items/qsgtextinput.cpp
tests/auto/declarative/qsgtextedit/data/cursorTest.qml
tests/auto/declarative/qsgtextedit/tst_qsgtextedit.cpp
tests/auto/declarative/qsgtextinput/data/cursorTest.qml
tests/auto/declarative/qsgtextinput/tst_qsgtextinput.cpp

index 6dd3908..cec3bd8 100644 (file)
@@ -891,7 +891,9 @@ void QSGTextEdit::loadCursorDelegate()
     Q_D(QSGTextEdit);
     if(d->cursorComponent->isLoading())
         return;
-    d->cursor = qobject_cast<QSGItem*>(d->cursorComponent->create(qmlContext(this)));
+    QDeclarativeContext *creationContext = d->cursorComponent->creationContext();
+    QObject *object = d->cursorComponent->create(creationContext ? creationContext : qmlContext(this));
+    d->cursor = qobject_cast<QSGItem*>(object);
     if(d->cursor){
         d->control->setCursorWidth(0);
         updateCursor();
@@ -900,6 +902,7 @@ void QSGTextEdit::loadCursorDelegate()
         d->cursor->setHeight(QFontMetrics(d->font).height());
         moveCursorDelegate();
     }else{
+        delete object;
         qmlInfo(this) << "Error loading cursor delegate.";
     }
 }
index 71c414f..3f1d95a 100644 (file)
@@ -971,8 +971,11 @@ void QSGTextInput::createCursor()
 
     if(d->cursorItem)
         delete d->cursorItem;
-    d->cursorItem = qobject_cast<QSGItem*>(d->cursorComponent->create());
+    QDeclarativeContext *creationContext = d->cursorComponent->creationContext();
+    QObject *object = d->cursorComponent->create(creationContext ? creationContext : qmlContext(this));
+    d->cursorItem = qobject_cast<QSGItem*>(object);
     if(!d->cursorItem){
+        delete object;
         qmlInfo(this, d->cursorComponent->errors()) << tr("Could not instantiate cursor delegate");
         return;
     }
index e734fc1..7bfc869 100644 (file)
@@ -1,8 +1,9 @@
 import QtQuick 2.0
 
 Rectangle { width: 300; height: 300; color: "white"
+    property string contextualProperty: "Hello"
     TextEdit {  text: "Hello world!"; id: textEditObject; objectName: "textEditObject"
-        resources: [ Component { id:cursor; Item { id:cursorInstance; objectName: "cursorInstance" } } ] 
+        resources: [ Component { id:cursor; Item { id:cursorInstance; objectName: "cursorInstance"; property string localProperty: contextualProperty } } ]
         cursorDelegate: cursor
     }
 }
index 1a9372b..8745229 100644 (file)
@@ -1589,6 +1589,7 @@ void tst_qsgtextedit::cursorDelegate()
     textEditObject->setFocus(true);
     QSGItem* delegateObject = textEditObject->findChild<QSGItem*>("cursorInstance");
     QVERIFY(delegateObject);
+    QCOMPARE(delegateObject->property("localProperty").toString(), QString("Hello"));
     //Test Delegate gets moved
     for(int i=0; i<= textEditObject->text().length(); i++){
         textEditObject->setCursorPosition(i);
index 01858fb..71a420e 100644 (file)
@@ -1,8 +1,9 @@
 import QtQuick 2.0
 
-Rectangle { width: 300; height: 300; color: "white"
+Rectangle { id:rect; width: 300; height: 300; color: "white"
+    property string contextualProperty: "Hello"
     TextInput {  text: "Hello world!"; id: textInputObject; objectName: "textInputObject"
-        resources: [ Component { id:cursor; Item { id:cursorInstance; objectName: "cursorInstance";} } ] 
+        resources: [ Component { id:cursor; Item { id:cursorInstance; objectName: "cursorInstance"; property string localProperty: contextualProperty } } ]
         cursorDelegate: cursor
     }
 }
index 26638c1..b2960dd 100644 (file)
@@ -1742,6 +1742,7 @@ void tst_qsgtextinput::cursorDelegate()
     textInputObject->setFocus(true);
     QSGItem* delegateObject = textInputObject->findChild<QSGItem*>("cursorInstance");
     QVERIFY(delegateObject);
+    QCOMPARE(delegateObject->property("localProperty").toString(), QString("Hello"));
     //Test Delegate gets moved
     for(int i=0; i<= textInputObject->text().length(); i++){
         textInputObject->setCursorPosition(i);