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

Cannot receive non-QObject* pointers when calling C++ methods from QML

    XMLWordPrintable

Details

    Description

      E.g. C++ class has

      class MyElement : public QObject
      {
      public:
          Q_INVOKABLE doStuff(MyElement *e) { qDebug() << e->objectName(); }
      }
      

      In QML:

      MyElement { id: obj }
      
      Component.onCompleted: obj.doStuff(obj)
      

      Running this QML crashes as the pointer received in doStuff() is invalid.

      Obviously MyElement has already been registered with qmlRegisterType(). Adding QML_DECLARE_TYPE(MyElement) made no difference.

      The workaround is to make the pointer type a QObject* instead and then qobject_cast it to a MyElement* object. The pointer is then valid.

      Attachments

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

        Activity

          People

            aakenned Aaron Kennedy
            bealam Bea Lam (closed Nokia identity) (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes