I have a class that inherits QQuickItem
and QQuickImageProvider
. This class is instantiated from qml. I need to call QQmlEngine::addImageProvider
so that this class can actually provide images. I should be able to get the engine that my object exists in as described in this question. In the constructor of my class I'm calling
QQmlEngine *engine = nullptr;
QQmlContext *context = QQmlEngine::contextForObject(this);
if (context)
engine = context->engine();
if (engine)
engine->addImageProvider("MyImageProvider", this);
But QQmlEngine::contextForObject(this);
always returns a null pointer. Why does this not work?
I figured it out. Calling
QQmlEngine::contextForObject(this);
in the constructor doesn't work, I'm guessing because the object isn't done being constructed yet. If I make aQ_INVOKABLE
function to register it laterand then in my QML:
Then that works like a charm.