summaryrefslogtreecommitdiffstats
path: root/src/declarative/qml/qdeclarativecomponent.cpp
diff options
context:
space:
mode:
authorAlan Alpert <alan.alpert@nokia.com>2010-05-05 18:54:19 (GMT)
committerAlan Alpert <alan.alpert@nokia.com>2010-05-05 18:54:19 (GMT)
commit0a8379d9f01118d7ff0121e6ecbbc0307e1e7f63 (patch)
treedbbf96df4245ecd37d9c9f0b619afaf6ddd19e38 /src/declarative/qml/qdeclarativecomponent.cpp
parent337dbd5391b4e2b1bd88918a46f3392df8fd1312 (diff)
downloadQt-0a8379d9f01118d7ff0121e6ecbbc0307e1e7f63.zip
Qt-0a8379d9f01118d7ff0121e6ecbbc0307e1e7f63.tar.gz
Qt-0a8379d9f01118d7ff0121e6ecbbc0307e1e7f63.tar.bz2
Make component.createObject require a parent argument
For graphical objects (the common case) a common mistake is to not parent a dynamically created item. Since you almost always want to add a parent, and it's hard for a beginner to diagnose this problem, a parent is now a required argument and dealt with by the createObject function. Task-number: QTBUG-10110
Diffstat (limited to 'src/declarative/qml/qdeclarativecomponent.cpp')
-rw-r--r--src/declarative/qml/qdeclarativecomponent.cpp20
1 files changed, 19 insertions, 1 deletions
diff --git a/src/declarative/qml/qdeclarativecomponent.cpp b/src/declarative/qml/qdeclarativecomponent.cpp
index 3ca0707..e7b9c9e 100644
--- a/src/declarative/qml/qdeclarativecomponent.cpp
+++ b/src/declarative/qml/qdeclarativecomponent.cpp
@@ -59,6 +59,7 @@
#include <QFileInfo>
#include <QtCore/qdebug.h>
#include <QApplication>
+#include <QGraphicsObject>
QT_BEGIN_NAMESPACE
@@ -557,8 +558,11 @@ QDeclarativeComponent::QDeclarativeComponent(QDeclarativeComponentPrivate &dd, Q
/*!
\internal
A version of create which returns a scriptObject, for use in script
+
+ Sets graphics object parent because forgetting to do this is a frequent
+ and serious problem.
*/
-QScriptValue QDeclarativeComponent::createObject()
+QScriptValue QDeclarativeComponent::createObject(QObject* parent)
{
Q_D(QDeclarativeComponent);
QDeclarativeContext* ctxt = creationContext();
@@ -567,6 +571,20 @@ QScriptValue QDeclarativeComponent::createObject()
QObject* ret = create(ctxt);
if (!ret)
return QScriptValue(QScriptValue::NullValue);
+
+ QGraphicsObject* gobj = qobject_cast<QGraphicsObject*>(ret);
+ bool needParent = (gobj != 0);
+ if(parent){
+ ret->setParent(parent);
+ QGraphicsObject* gparent = qobject_cast<QGraphicsObject*>(parent);
+ if(gparent){
+ gobj->setParentItem(gparent);
+ needParent = false;
+ }
+ }
+ if(needParent)
+ qWarning("QDeclarativeComponent: Created graphical object was not placed in the graphics scene.");
+
QDeclarativeEnginePrivate *priv = QDeclarativeEnginePrivate::get(d->engine);
QDeclarativeData::get(ret, true)->setImplicitDestructible();
return priv->objectClass->newQObject(ret, QMetaType::QObjectStar);