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

A QML import alias fails silently

    XMLWordPrintable

Details

    • Linux/Other display system

    Description

      I have created two C++ classes in different namespaces with the same name, GenericActions. Both classes inherit QObject, make use of Q_INVOKABLE to be used in QML, and they're registered as singletons in different parts of the code :

          qmlRegisterSingletonType<GenericActions> ("ModuleA", versionMajor, versionMinor, "GenericActions", SingletonRegisterCallback_Helper<GenericActions>);
      
      // ... and somewhere else (different file)...
      
          qmlRegisterSingletonType<GenericActions> ("ModuleB.ActionsSubmodule", versionMajor, versionMinor, "GenericActions", SingletonRegisterCallback_Helper<GenericActions>);
      

      The first one existed previously and has been used for a while, I was trying to add the second one. Then in a QML file which uses both modules, I wrote at the top :

      import ModuleA
      import ModuleB.ActionsSubmodule as ModuleBActions
      ...
      

      Then I ran into some JS "undefined" nonsense, and finally found out that the alias seemed broken. ModuleA.GenericActions should have a "propertyForModuleA" property, and ModuleB.ActionsSubmodule.GenericActions should have a "propertyForModuleB" property. But when I print out things like this:

                      console.log("Non-alias import from ModuleA")
                      console.log(GenericActions)
                      console.log("Aliased import from ModuleB")
                      console.log(ModuleBActions.GenericActions)
                      console.log("Another aliased import from ModuleB (not duplicated in ModuleA)")
                      console.log(ModuleBActions.ErrorManagementActions)
                      console.log("The ModuleBActions namespace exists")
                      console.log(ModuleBActions)
                      console.log("The class from ModuleA should have a propertyForModuleA member and no propertyForModuleB member")
                      console.log(GenericActions.propertyForModuleA, GenericActions.propertyForModuleB)
                      console.log("The class from ModuleB does not have a propertyForModuleA member but has propertyForModuleB")
                      console.log(ModuleBActions.GenericActions.propertyForModuleA, ModuleBActions.GenericActions.propertyForModuleB)
      

      ... I get :

      qml: Non-alias import from ModuleA
      qml: GenericActions(0x23c9aa0)
      qml: Aliased import from ModuleB
      QQmlEngine::setContextForObject(): Object already has a QQmlContext
      qml: GenericActions(0x23c9aa0)
      qml: Another aliased import from ModuleB (not duplicated in ModuleA)
      qml: ErrorManagementActions(0x266cef0)
      qml: The ModuleBActions namespace exists
      qml: [object Object]
      qml: The class from ModuleA should have a propertyForModuleA member and no propertyForModuleB member
      qml: anato::qtflux::AsyncActionCreator(0x263fc90) undefined
      qml: The class from ModuleB does not have a propertyForModuleA member but has propertyForModuleB
      qml: anato::qtflux::AsyncActionCreator(0x263fc90) undefined
      

      As you can see, both singleton objects seem to be the same, with hash/address 0x23c9aa0 - and judging from the last prints, this is the class from the QML ModuleA module.

      I have no idea what is going on here, but if I did something wrong the QML engine should at the very least produce an error instead of this faulty behaviour.

      Attachments

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

        Activity

          People

            qtqmlteam Qt Qml Team User
            graslany Yves Grasland
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes