Deprecate QQmlExtensionPlugin::baseUrl()

The function does not play well with optional plugins as we don't need
to load those from any specific location. You also don't need this
function. There is exactly one place in qtdeclarative where it is used
and there it just provides a poor replacement for qmldir entries.

[ChangeLog][QtQml][Important Behavior Changes] The internal function
QQmlExtensionPlugin::baseUrl() has been deprecated. You don't need it
if you properly specify your module in the qmldir file.

Change-Id: I131c45e069e9c10744a0ad6126604a2600a6d93d
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Ulf Hermann 2021-06-23 09:57:23 +02:00
parent d6c82d9251
commit a5404b2cb6
7 changed files with 27 additions and 2 deletions

View File

@ -75,7 +75,11 @@ QT_BEGIN_NAMESPACE
\internal
*/
QQmlExtensionPlugin::QQmlExtensionPlugin(QObject *parent)
#if QT_DEPRECATED_SINCE(6, 3)
: QObject(*(new QQmlExtensionPluginPrivate), parent)
#else
: QObject(parent)
#endif
{
}
@ -102,19 +106,31 @@ QQmlExtensionPlugin::~QQmlExtensionPlugin() = default;
*/
QQmlEngineExtensionPlugin::~QQmlEngineExtensionPlugin() = default;
#if QT_DEPRECATED_SINCE(6, 3)
/*!
\since 5.1
\internal
\deprecated [6.3] This is unnecessary and doesn't work for optional plugins
\brief Returns the URL of the directory from which the extension is loaded.
This is useful when the plugin also needs to load QML files or other
assets from the same directory.
\note You should not need this function. Other files that are part of the
module's public interface should be specified accordingly in the build
system and qmldir file. The build system makes sure that they end up
both in the final module directory, and in the resource file system.
You can use the copy from the resource file system in the plugin.
Non-QML/JS files private to the plugin can be added to the resource
file system manually. However, consider moving all such functionality
out of the plugin and making the plugin optional.
*/
QUrl QQmlExtensionPlugin::baseUrl() const
{
Q_D(const QQmlExtensionPlugin);
return d->baseUrl;
}
#endif
/*!
\since 6.0

View File

@ -61,7 +61,10 @@ public:
explicit QQmlExtensionPlugin(QObject *parent = nullptr);
~QQmlExtensionPlugin() override;
#if QT_DEPRECATED_SINCE(6, 3)
QT_DEPRECATED_VERSION_X_6_3("Provide a qmldir file to remove the need for calling baseUrl")
QUrl baseUrl() const;
#endif
void registerTypes(const char *uri) override = 0;
virtual void unregisterTypes();

View File

@ -56,6 +56,7 @@
QT_BEGIN_NAMESPACE
#if QT_DEPRECATED_SINCE(6, 3)
class QQmlExtensionPluginPrivate : public QObjectPrivate
{
Q_DECLARE_PUBLIC(QQmlExtensionPlugin)
@ -66,6 +67,7 @@ public:
QUrl baseUrl;
};
#endif
QT_END_NAMESPACE

View File

@ -845,11 +845,13 @@ QQmlMetaType::RegistrationResult QQmlMetaType::registerPluginTypes(
return RegistrationResult::Failure;
}
#if QT_DEPRECATED_SINCE(6, 3)
if (auto *plugin = qobject_cast<QQmlExtensionPlugin *>(instance)) {
// basepath should point to the directory of the module, not the plugin file itself:
QQmlExtensionPluginPrivate::get(plugin)->baseUrl
= QQmlImports::urlFromLocalFileOrQrcOrUrl(basePath);
}
#endif
const QByteArray bytes = uri.toUtf8();
const char *moduleId = bytes.constData();

View File

@ -42,8 +42,7 @@ public:
void registerTypes(const char *uri) override
{
Q_ASSERT(QLatin1String(uri) == "org.qtproject.ModuleWithQmlSingleton");
qmlRegisterSingletonType(baseUrl().resolved(QUrl("ModuleWithQmlSingleton/MySingleton.qml")), uri, 1, 0, "MySingleton");
qmlRegisterSingletonType(baseUrl().resolved(QUrl("ModuleWithQmlSingleton/MySingleton2.qml")), uri, 1, 0, "MySingleton2");
qmlRegisterModule(uri, 1, 0);
}
};

View File

@ -1,2 +1,4 @@
module org.qtproject.ModuleWithQmlSingleton
plugin moduleWithQmlSingleton
singleton MySingleton 1.0 MySingleton.qml
singleton MySingleton2 1.0 MySingleton2.qml

View File

@ -809,6 +809,7 @@ void tst_qqmlmoduleplugin::multiSingleton()
qmlRegisterSingletonInstance("Test", 1, 0, "Tracker", &obj);
engine.addImportPath(m_importsDirectory);
QQmlComponent component(&engine, testFileUrl("multiSingleton.qml"));
QVERIFY2(component.isReady(), qPrintable(component.errorString()));
QObject *object = component.create();
QVERIFY(object != nullptr);
QCOMPARE(obj.objectName(), QLatin1String("first"));