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 \internal
*/ */
QQmlExtensionPlugin::QQmlExtensionPlugin(QObject *parent) QQmlExtensionPlugin::QQmlExtensionPlugin(QObject *parent)
#if QT_DEPRECATED_SINCE(6, 3)
: QObject(*(new QQmlExtensionPluginPrivate), parent) : QObject(*(new QQmlExtensionPluginPrivate), parent)
#else
: QObject(parent)
#endif
{ {
} }
@ -102,19 +106,31 @@ QQmlExtensionPlugin::~QQmlExtensionPlugin() = default;
*/ */
QQmlEngineExtensionPlugin::~QQmlEngineExtensionPlugin() = default; QQmlEngineExtensionPlugin::~QQmlEngineExtensionPlugin() = default;
#if QT_DEPRECATED_SINCE(6, 3)
/*! /*!
\since 5.1 \since 5.1
\internal \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. \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 This is useful when the plugin also needs to load QML files or other
assets from the same directory. 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 QUrl QQmlExtensionPlugin::baseUrl() const
{ {
Q_D(const QQmlExtensionPlugin); Q_D(const QQmlExtensionPlugin);
return d->baseUrl; return d->baseUrl;
} }
#endif
/*! /*!
\since 6.0 \since 6.0

View File

@ -61,7 +61,10 @@ public:
explicit QQmlExtensionPlugin(QObject *parent = nullptr); explicit QQmlExtensionPlugin(QObject *parent = nullptr);
~QQmlExtensionPlugin() override; ~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; QUrl baseUrl() const;
#endif
void registerTypes(const char *uri) override = 0; void registerTypes(const char *uri) override = 0;
virtual void unregisterTypes(); virtual void unregisterTypes();

View File

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

View File

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

View File

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

View File

@ -1,2 +1,4 @@
module org.qtproject.ModuleWithQmlSingleton module org.qtproject.ModuleWithQmlSingleton
plugin 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); qmlRegisterSingletonInstance("Test", 1, 0, "Tracker", &obj);
engine.addImportPath(m_importsDirectory); engine.addImportPath(m_importsDirectory);
QQmlComponent component(&engine, testFileUrl("multiSingleton.qml")); QQmlComponent component(&engine, testFileUrl("multiSingleton.qml"));
QVERIFY2(component.isReady(), qPrintable(component.errorString()));
QObject *object = component.create(); QObject *object = component.create();
QVERIFY(object != nullptr); QVERIFY(object != nullptr);
QCOMPARE(obj.objectName(), QLatin1String("first")); QCOMPARE(obj.objectName(), QLatin1String("first"));