Important: Please read the Qt Code of Conduct -

[SOLVED] Proper way of creating complex QDeclarativeItem class exposed to qml by QDeclarativeExtensionPlugin

  • Hello all,
    I need some expertise.

    Let's assume that I have class A that inherits from QDeclarativeItem. I want this item to be composed from few QDeclarativeComponent's created in A constructor. I need pointer tor QDeclarativeEngine for this.

    I've achieved this by:

    QmlPlugin: public QDeclarativeExtensionPlugin {
    static QDeclarativeEngine * engine;
    void initializeEngine ( QDeclarativeEngine * _engine, const char * uri ) {
    engine = _engine;

    Now I can get QDeclarativeEngine * from inside contructor of A class (QmlPlugin::engine), but...

    • If I have 2 QDeclarativeView's that load 2 qml files that import this plugin, will it create 2 QDeclarativeEngine's and only one will be stored in this static variable?
    • Is there more proper way of doing this?

  • Hi,

    From within the class A constructor, you should be able to call qmlEngine(this) to get the engine that constructed the item. (I'm not sure if this function is documented -- you could also use QDeclarativeEngine::contextForObject(this)->engine())


  • Hello,
    I supposed that there is more convenient way for getting pointer to QDeclarativeEngine from inside the plugin. As far as I know qmlEngine function is not documented but QDeclarativeEngine::contextForObject I have skipped somehow.
    Thank You for hints!

  • qmlEngine and QDeclarativeEngine::contextForObject returns NULL in class A constructor.
    I've done creating additional internal components in A::onCompleted - there i can get pointer to engine from qmlEngine function.

Log in to reply