-
Bug
-
Resolution: Unresolved
-
P2: Important
-
6.2.2
;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.