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

Namespace name has to be explicitly used for QML compatibility

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • P3: Somewhat important
    • 5.0.0
    • 4.7.1
    • None
    • Windows XP Professional (Version 2002, Service Pack 3) and Mac Os X Snow Leopard (Version 10.6.5).

    • macOS
    • 32b595feb5d5dcaf9cdea9c1f8ce59ac151fb8cd

    Description

      If a class is declared inside a namespace, then the namespace name has to be included when referencing to the class in Q_PROPERTY declarations and in associated read and write function prototypes and, also, in Q_INVOKABLE function prototypes. For example, let's consider two classes Bar and Baz that are declared in namespace Foo:

      namespace Foo {
      
          class Baz : public QObject
          {
              ...
          };
      
          class Bar : public QObject
          {
              Q_OBJECT
              Q_PROPERTY(Foo::Baz* baz READ baz CONSTANT)
      //        Q_PROPERTY(Baz* baz READ baz CONSTANT)
          public:
              ...
              Foo::Bar* baz() const;
      //        Bar* baz() const;
              Q_INVOKABLE Foo::Bar* getBaz() const;
      //        Q_INVOKABLE Bar* getBaz() const;
              ...
          };
      
      } /* namespace Foo */
      

      As mentioned above, the namespace names in front of class name Baz are necessary for the classes to be usable in QML code. If the namespace names are removed from the declaration of class Bar (commented lines), the QML code using the classes does not work as expected. See the attached application for an example. In general, C++ does not have this kind of limitation; it is enough that the class declarations are inside the namespace and there is no need to use namespace names. The suggestion is that the QML documentation is updated so that this issue is made clearer for the reader. It would help developers who use namespaces and encounter "not-an-object" error messages.

      Attachments

        1. foo.cpp
          0.4 kB
        2. foo.h
          0.8 kB
        3. main.cpp
          0.4 kB
        4. namespace.pro
          0.2 kB
        5. namespace.qrc
          0.1 kB
        6. namespace.zip
          2 kB
        7. qtbug15459-doc-example.zip
          2 kB
        8. ui.qml
          0.3 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            mitch_curtis Mitch Curtis
            phn P-H Niemenlehto
            Votes:
            2 Vote for this issue
            Watchers:
            10 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes