Doc: Warn about shadowing URLs when dynamically creating components

By passing the URL of an existing component you can do interesting
things with QQmlComponent and Qt.createQmlObject. You really shouldn't
do that.

Pick-to: 6.9 6.8 6.5
Fixes: QTBUG-119545
Change-Id: Iadad0bacc3269be757b4d02845f9a8d0ec12d39a
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Ulf Hermann 2025-02-18 15:52:37 +01:00
parent a64f6a207c
commit d278c23380
2 changed files with 14 additions and 5 deletions

View File

@ -118,7 +118,7 @@ of their use.
\list
\li \l{Qt::createComponent()}{object Qt.createComponent(url)}
\li \l{Qt::createQmlObject()}{object Qt.createQmlObject(string qml, object parent, string filepath)}
\li \l{Qt::createQmlObject()}{object Qt.createQmlObject(string qml, object parent, url url)}
\endlist
@ -1277,12 +1277,18 @@ void QtObject::exit(int retCode) const
}
/*!
\qmlmethod object Qt::createQmlObject(string qml, object parent, string filepath)
\qmlmethod object Qt::createQmlObject(string qml, object parent, url url)
Returns a new object created from the given \a qml string which will have the specified \a parent,
or \c null if there was an error in creating the object.
Compiles the given \a qml string into a component and then returns a new object created from
that component. The new object will have the specified \a parent. Returns \c null if there was
an error in creating the component or the object.
If \a filepath is specified, it will be used for error reporting for the created object.
If \a url is specified, it will be used as URL of the component. This is useful for error
reporting.
\warning The new component will shadow any existing component of the same URL. You should not
pass a URL of an existing component. In particular, by passing the URL of the surrounding QML
file, you prevent access to the surrounding component from the new one.
Example (where \c parentItem is the id of an existing QML item):

View File

@ -707,6 +707,9 @@ QQmlComponent::QQmlComponent(QQmlEngine *engine, QV4::ExecutableCompilationUnit
Sets the QQmlComponent to use the given QML \a data. If \a url
is provided, it is used to set the component name and to provide
a base path for items resolved by this component.
\warning The new component will shadow any existing component of
the same URL. You should not pass a URL of an existing component.
*/
void QQmlComponent::setData(const QByteArray &data, const QUrl &url)
{