Details
-
Bug
-
Resolution: Fixed
-
P3: Somewhat important
-
5.15, 6.0, 6.1
-
7e6979ca6fe3193003f9031027210a18a8e5f1fb
Description
The following code (complete minimal project attached) produces no errors even though it attempts to register a C++ singleton and a QML singleton under the same import name:
main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> class Logging : public QObject { Q_OBJECT public: Logging() = default; }; QObject *loggingSingletonTypeProvider(QQmlEngine *, QJSEngine *) { return new Logging(); } int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); qmlRegisterSingletonType<Logging>("Foo", 1, 0, "Logging", loggingSingletonTypeProvider); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); } #include "main.moc"
main.qml
import QtQuick 2.12 import QtQuick.Window 2.12 import Foo 1.0 import "logging" Window { visible: true width: 640 height: 480 title: qsTr("Hello World") Component.onCompleted: console.log(Logging.main, "this is a message from main") }
The only indication that something is wrong (this is part of a much larger application) is that the logging category name is printed as "undefined", since no such property exists in the C++ singleton.
I would expect that an error is produced when trying to import the "logging" QML singleton. E.g.:
Cannot import QML module "logging" as a singleton named "Logging" was already registered in C++ via qmlRegisterSingletonType().