Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-82301

No error when registering C++ singleton and QML singleton into the same module name

    XMLWordPrintable

Details

    • 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().
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            ulherman Ulf Hermann
            mitch_curtis Mitch Curtis
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes