Qt World Summit: Submit your Presentation

How to get QQmlEngine from QQuickItem

  • I have a QQuickItem, I want to create QQuickText items dynamically in it according to the text property. Because this QQuickItem is a kind of plugin, so I can't get the QQmlEngine directly from the view(I don't know who will use it, I want it to hehave like a normal qml plugin), how to get QQmlEngine from QQuickItem so I can use QQmlComponent to create QQuickText?

    Bye the way,can I create QQuickText directly? can I use the private header files?

  • Try:

    QQmlEngine *engine = QQmlEngine::contextForObject(quickItemPointer)->engine();

    Not sure if there is a more direct way to do this. If there isn't then I think there ought to be.

  • Hi,

    qmlEngine(objectPtr) and qmlContext(objectPtr) are useful for this. They are public, and documented, but for some reason the docs aren't showing up the related methods section of the QQmlEngine documentation.

    Yes, you can include the _p.h headers and use the private types; just include "QT+=quick-private" in your .pro. However, doing that means that you are tied to that particular version of Qt, as there are no binary or source guarantees provided for the private classes.


  • Yeah I thought there must be a more direct approach to this, I just couldn't find anything in the docs. Thanks Chris.

Log in to reply