From b3f9f53ff12d7fd5fc5b667c6a58169d5de06939 Mon Sep 17 00:00:00 2001 From: Andrew den Exter Date: Fri, 30 Sep 2011 17:17:49 +1000 Subject: [PATCH] Provide a context when constructing a TextInput cursor delegate. 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 Reviewed-by: Aaron Kennedy --- src/declarative/items/qsgtextedit.cpp | 5 ++++- src/declarative/items/qsgtextinput.cpp | 5 ++++- tests/auto/declarative/qsgtextedit/data/cursorTest.qml | 3 ++- tests/auto/declarative/qsgtextedit/tst_qsgtextedit.cpp | 1 + tests/auto/declarative/qsgtextinput/data/cursorTest.qml | 5 +++-- tests/auto/declarative/qsgtextinput/tst_qsgtextinput.cpp | 1 + 6 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/declarative/items/qsgtextedit.cpp b/src/declarative/items/qsgtextedit.cpp index 6dd3908..cec3bd8 100644 --- a/src/declarative/items/qsgtextedit.cpp +++ b/src/declarative/items/qsgtextedit.cpp @@ -891,7 +891,9 @@ void QSGTextEdit::loadCursorDelegate() Q_D(QSGTextEdit); if(d->cursorComponent->isLoading()) return; - d->cursor = qobject_cast(d->cursorComponent->create(qmlContext(this))); + QDeclarativeContext *creationContext = d->cursorComponent->creationContext(); + QObject *object = d->cursorComponent->create(creationContext ? creationContext : qmlContext(this)); + d->cursor = qobject_cast(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."; } } diff --git a/src/declarative/items/qsgtextinput.cpp b/src/declarative/items/qsgtextinput.cpp index 71c414f..3f1d95a 100644 --- a/src/declarative/items/qsgtextinput.cpp +++ b/src/declarative/items/qsgtextinput.cpp @@ -971,8 +971,11 @@ void QSGTextInput::createCursor() if(d->cursorItem) delete d->cursorItem; - d->cursorItem = qobject_cast(d->cursorComponent->create()); + QDeclarativeContext *creationContext = d->cursorComponent->creationContext(); + QObject *object = d->cursorComponent->create(creationContext ? creationContext : qmlContext(this)); + d->cursorItem = qobject_cast(object); if(!d->cursorItem){ + delete object; qmlInfo(this, d->cursorComponent->errors()) << tr("Could not instantiate cursor delegate"); return; } diff --git a/tests/auto/declarative/qsgtextedit/data/cursorTest.qml b/tests/auto/declarative/qsgtextedit/data/cursorTest.qml index e734fc1..7bfc869 100644 --- a/tests/auto/declarative/qsgtextedit/data/cursorTest.qml +++ b/tests/auto/declarative/qsgtextedit/data/cursorTest.qml @@ -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 } } diff --git a/tests/auto/declarative/qsgtextedit/tst_qsgtextedit.cpp b/tests/auto/declarative/qsgtextedit/tst_qsgtextedit.cpp index 1a9372b..8745229 100644 --- a/tests/auto/declarative/qsgtextedit/tst_qsgtextedit.cpp +++ b/tests/auto/declarative/qsgtextedit/tst_qsgtextedit.cpp @@ -1589,6 +1589,7 @@ void tst_qsgtextedit::cursorDelegate() textEditObject->setFocus(true); QSGItem* delegateObject = textEditObject->findChild("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); diff --git a/tests/auto/declarative/qsgtextinput/data/cursorTest.qml b/tests/auto/declarative/qsgtextinput/data/cursorTest.qml index 01858fb..71a420e 100644 --- a/tests/auto/declarative/qsgtextinput/data/cursorTest.qml +++ b/tests/auto/declarative/qsgtextinput/data/cursorTest.qml @@ -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 } } diff --git a/tests/auto/declarative/qsgtextinput/tst_qsgtextinput.cpp b/tests/auto/declarative/qsgtextinput/tst_qsgtextinput.cpp index 26638c1..b2960dd 100644 --- a/tests/auto/declarative/qsgtextinput/tst_qsgtextinput.cpp +++ b/tests/auto/declarative/qsgtextinput/tst_qsgtextinput.cpp @@ -1742,6 +1742,7 @@ void tst_qsgtextinput::cursorDelegate() textInputObject->setFocus(true); QSGItem* delegateObject = textInputObject->findChild("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); -- 2.7.4