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

Cannot assign null to interface property

    XMLWordPrintable

Details

    Description

       ;I have the following setup:

      MyInterface.h

      struct MyInteface {
        Q_GADGET
        QML_INTERFACE
      
      public:
        virtual ~MyInterface();
        virtual int getAnswer() const = 0;
      };
      
      Q_DECLARE_INTERFACE(MyInterface, "com.mycompany.MyInterface")

       

      MyConsumer.h

      class MyConsumer : public QObject {
        Q_OBJECT
        Q_PROPERTY(MyInterface* i READ i WRITE setI NOTIFY iChanged FINAL)
      
      public:
        using QObject::QObject;
      
        MyInterface* i() const { return m_interface; }
        void setI(MyInterface* i) { 
          if (i == m_interface) return;
          m_interface = i;
          emit iChanged();
        }
      
      signals:
        void iChanged();
      
      private:
       MyInterface* m_interface{nullptr};
      }

      MyImplementation.h

      class MyImplementation : public QObject, public MyInterface {
        Q_OBJECT
        QML_UNCREATABLE("Created in C++")
        QML_IMPLEMENTS_INTERFACES(MyInterface)
      
      public:
        using QObject::QObject;
      
        int getAnswer() const override { return 42; }
      };

       

      Provider.h

       

      class MyProvider : public QObject {
        Q_OBJECT
        QML_ELEMENT
       
        Q_PROPERTY(MyImplementation* impl READ impl NOTIFY implChanged FINAL)
      
      public:
        using QObject::QObject;
      
        MyImplementation* impl() const { return m_impl; }
      
      signals:
        void implChanged();
      
      private:
        MyImplementation* m_impl{nullptr}; // created later
      };

      main.qml

      ApplicationWindow {
        // some config
        MyProvider { id: provider }
        MyConsumer { i: provider.impl } // prints warning: Unable to assign null to MyInterface*
      }

      If the provider returns a valid instance no warning is printed. In my scenario the impl only becomes available later.

       

       

      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
            marcel.gotsch Marcel Gotsch
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes