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

Q_PROPERTY READ returning pointers to the property's type are confusing

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 5.0.2, 5.1.0
    • None
    • Windows 7, 8

    Description

      Hello, so there is a curious issue concerning Q_PROPERTY definitions with READ methods that return a reference to the read object rather than a copy of it. Here's what the docs claim concerning READ methods...

      "A READ accessor function is required. It is for reading the property value. Ideally, a const function is used for this purpose, and it must return either the property's type or a pointer or reference to that type"

      And yet Thiago's comment in the following bug seems completely contradictory to the part of that statement concerning the returning of references:

      https://bugreports.qt-project.org/browse/QTBUG-7407

      I ran across this issue by chance when exporting custom C++ classes to qml with the following error:

      "QMetaProperty::read: Unable to handle unregistered datatype..."

      The following are examples of the multiple ways I attempted to circumvent this error. When I found that the pointer solution worked while a reference version would not, I could tell something was wrong under the hood.

      Class1 {
      ...
      Q_PROPERTY(Class2 class2 READ getClass2)
      ...
      Class2& getClass2() {return m_class2;}
      ...
      private:
      Class2 m_class2;
      }
      

      When accessing class2 property in qml returns QMetaProperty::read error

      Class1 {
      ...
      Q_PROPERTY(Class2& class2 READ getClass2)
      ...
      Class2& getClass2() {return m_class2;}
      ...
      private:
      Class2 m_class2;
      }
      

      Produces moc error

      Class1 {
      ...
      Q_PROPERTY(Class2* class2 READ getClass2)
      ...
      Class2* getClass2() {return &m_class2;}
      ...
      private:
      Class2 m_class2;
      }
      

      Works as expected.

      Either the docs should be updated noting the deficiency in Q_PROPERTY's ability to handle READ methods which return references to avoid confusing users, or the backend should be appropriately updated to fix the root issue.

      Thank you,
      David

      Attachments

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

        Activity

          People

            ogoffart Olivier Goffart (Woboq GmbH)
            dchamberlain David Chamberlain
            Votes:
            3 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes