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

A pointer to a QObject derived user type within a namespace cannot be used as the return type of a Q_INVOKABLE function unless the fully qualified type name is used.

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 5.9.6, 5.12.0
    • Build tools: moc, Other
    • None
    • Linux/X11

    Description

      The Issue In Brief

      Given a QObject derived type defined within a namespace...

      namespace MyNamespace {
      class MyClass : public QObject
      {
      ...
      };
      }
      

      If a Q_INVOKABLE function returns a pointer to MyClass to the QmlEngine...

      namespace MyNamespace {
      class Controller : public QObject
      {
          ...
      
          Q_INVOKABLE MyClass* getMyClass();
      }
      };
      

      The QmlEngine does not recognize the return type. Console output:
      "Error: Unknown method return type: MyClass*"

      This occurs even though both MyClass and Controller are properly registered types:

      qmlRegisterSingletonType<MyNamespace::Controller>(uri, 1, 0, "Controller", &MyNamespace::Controller::qmlInstance);
          qmlRegisterUncreatableType<MyNamespace::MyClass>(uri, 1, 0, "MyClass", "Get instances via Controller API");
      

      Workaround

      I discovered that if I use a fully qualified type name as the return type for the Q_INVOKABLE function, then the issue is avoided.

      namespace MyNamespace {
      class Controller : public QObject
      {
          ...
      
          Q_INVOKABLE MyNamespace::MyClass* getMyClass();
      }
      };
      

      I have uploaded a minimal sample plugin and demo app that demonstrates this issue and the workaround.

      Attachments

        Issue Links

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

          Activity

            People

              ogoffart Olivier Goffart (Woboq GmbH)
              k__p Kevin Peizner
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes